Results 1 to 11 of 11

Thread: Excel Macro Question

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Excel Macro Question

    I have a very simple Excel macro consisting of

    Rows("271:271").Select
    Selection.Copy
    Selection.Insert Shift:=xlDown

    How can I change it so that it selects the currently selected row rather than row 271?

    The currently selected row will always be the row above the first row of some totals, so ideally I'd like the macro to find the total row and do the copy and insert always using the row above it.

  2. #2
    Fanatic Member Avatarp's Avatar
    Join Date
    Sep 2002
    Location
    Calgary
    Posts
    826

    Martin Liss?

    Set some Range variable and search the Range variable for the type of formating in that Totals row. The count will tell you what row that is. Then you can easyly set the copy and paste ranges.

    do you need me to show you how to code this?

    You should post this in the VBA forum

  3. #3
    Fanatic Member Avatarp's Avatar
    Join Date
    Sep 2002
    Location
    Calgary
    Posts
    826

    get the current cell position simply do this

    Code:
    SelectedRow = ActiveCell.Row
    SelectedColumn = ActiveCell.Column

  4. #4

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Martin Liss?

    Originally posted by Avatarp
    Set some Range variable and search the Range variable for the type of formating in that Totals row. The count will tell you what row that is. Then you can easyly set the copy and paste ranges.

    do you need me to show you how to code this?

    You should post this in the VBA forum
    Yes, thanks.

    BTW, I moved the thread.

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Marty,

    I find this usefull too:
    VB Code:
    1. 'Use UsedRange like:
    2.  
    3.     intRowCount = objExcel.ActiveSheet.UsedRange.Rows.Count 'Capture the number of USED Rows
    4.     intColCount = objExcel.ActiveSheet.UsedRange.Columns.Count  'Capture the number of USED Columns

    In your case, if no cell is selected, then use
    VB Code:
    1. ActiveSheet.UsedRange.Rows.Count [b]- 1[/b]




    Bruce.

  6. #6

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    The first column of my excel spreadsheet looks like this

    10
    -15
    120
    -5
    Total: 110

    The macro I want would atomatically copy the -5 row and insert it under that row so it would look like this.


    10
    -15
    120
    -5
    -5
    Total: 110

    I would then manually update the 2nd -5 row.

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Who is asking?

    OK, you want to select the entire row of the actual cell, and paste it into the sheet again (it will be pasted above the actual one, but I think that doesn't matter).
    VB Code:
    1. Public Sub Martin()
    2.     Rows(ActiveCell.Row).Select
    3.     Selection.Copy
    4.     Selection.Insert
    5. End Sub
    After that you want to manually update the new row(probably putting in correct data) but what about the totals row.
    In your example the value of totals didn't change, I guess you want the totals of all cells above (i.e. in your example "105"). If that's correct, EXCEL will do the trick for you.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8
    Fanatic Member Avatarp's Avatar
    Join Date
    Sep 2002
    Location
    Calgary
    Posts
    826

    Here is a solution....

    Assign a buttons Macro to the follow sub-routine

    Code:
    Private Sub test()
        ActiveRow = ActiveCell.Row
        ActiveCol = ActiveCell.Column
        
        For i = 1 To 5000
            TotalFindCell = Workbooks("Book1.xls").Sheets("Sheet1").Cells(i, 1)
            If InStr(1, TotalFindCell, "Total") Then
                Workbooks("Book1.xls").Sheets("Sheet1").Cells(i, 1).Select
                ActiveRow = ActiveCell.Row
                Cells(ActiveRow, 1).Select
                Selection.EntireRow.Insert
                Workbooks("Book1.xls").Sheets("Sheet1").Cells(i - 1, 1).Select
                Selection.Copy
                Workbooks("Book1.xls").Sheets("Sheet1").Cells(i, 1).Select
                ActiveSheet.Paste
                Application.CutCopyMode = False
                Exit For
            End If
        Next
        Cells(ActiveRow, ActiveCol).Select
    End Sub

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Here is a solution....

    Originally posted by Avatarp
    Assign a buttons Macro to the follow sub-routine

    Code:
         For i = 1 To 5000
            '
            '
        Next

    G'Day Avatarp,

    There is NO need to pick a random high number to iterate all cells in that row (or column). I posted a method above to take care of the used rows/columns. Dont waste time passing across cells that arn't used!




    Bruce.

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    Add the Checkmark and the word [RESOLVED] to the subject of the first post in your thread
    if your question has been answered satisfactorily.

  11. #11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width