+ Reply to Thread
Results 1 to 12 of 12

macro to paste last active rows data into "master"

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-26-2010
    Location
    usa
    MS-Off Ver
    Office 365
    Posts
    1,233

    macro to paste last active rows data into "master"

    hello

    i have the following macro in "my workbook"

    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Dim ofs As Long
    If Sh.Name Like "P*" And Sh.Name <> Sheet17.Name Then
        ofs = CLng(Mid(Sh.Name, 2)) - 1
        Sheets("Master").Range("B10:P1009").Offset(ofs * 1000).Value = Sh.Range("B10:P1009").Value
    End If
    End Sub
    what i would like...if in the range B10:P1009, the data is last reported on row 696, can the macro only consolidate the data in the master sheet of the active cells i.e. B10:P696?

    my data is consolidated into the "master" worksheet, starting in cell B10....

    can someone pls help!

  2. #2
    Forum Contributor
    Join Date
    11-26-2010
    Location
    usa
    MS-Off Ver
    Office 365
    Posts
    1,233

    Re: macro to paste last active rows data into "master"

    any thoughts guys?!? thxs

  3. #3
    Forum Contributor
    Join Date
    11-26-2010
    Location
    usa
    MS-Off Ver
    Office 365
    Posts
    1,233

    Re: macro to paste last active rows data into "master"

    any thoughts or updates guys? thx u

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro to paste last active rows data into "master"

    Are you consolidating data from multiple worksheets or multiple workbooks?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Forum Contributor
    Join Date
    11-26-2010
    Location
    usa
    MS-Off Ver
    Office 365
    Posts
    1,233

    Re: macro to paste last active rows data into "master"

    hello

    yes i am.

    the worksheets i am consolidating my data from are P1, P2, P3...P12

    also, in my master sheet where the data is being consolidated, column A is a # cell i.e. A10 = 1, A11 = 2, A12 = 3 etc

    ur thoughts? thx u

  6. #6
    Forum Contributor
    Join Date
    11-26-2010
    Location
    usa
    MS-Off Ver
    Office 365
    Posts
    1,233

    Re: macro to paste last active rows data into "master"

    hello all

    any thoughts?? thxs

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro to paste last active rows data into "master"

    You can try this code
    Sub copy_data()
    Dim i As Long
    Dim lrow As Long
    
    For i = 1 To Worksheets.Count
        With Worksheets(i)
            If .Name Like "P*" Then
                lrow = .Range("B" & .Rows.Count).End(xlUp).Row
                .Range("B10:P" & lrow).Copy Worksheets("Master").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
            End If
        End With
    Next i
    
    End Sub
    What is the name of your Sheet17? You have specified that as a condition. I have not included it, since i didnt know what the sheetname was. What if the sheet no changes to 18? In that case, its better to specify the name in the condition, like if .name <>"Test".

  8. #8
    Forum Contributor
    Join Date
    11-26-2010
    Location
    usa
    MS-Off Ver
    Office 365
    Posts
    1,233

    Re: macro to paste last active rows data into "master"

    hello quick question and thx u so much for your help!!

    if for instance in my worksheet "P1"...where the data is being extracted from into the master sheet, contains one blank row...will the macro automatically adjust to pick up the next row or will it jump into the next sheet "P2" and extract data from there? thx u so much for your kind help.

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro to paste last active rows data into "master"

    The macro will check for the last row with data in column B in each worksheet beginning with P. It does this by going to the last row in excel and climbing up to the last row with data. If there are blanks in between, it will include that as well. Then, it will move to the next worksheet.

  10. #10
    Forum Contributor
    Join Date
    11-26-2010
    Location
    usa
    MS-Off Ver
    Office 365
    Posts
    1,233

    Re: macro to paste last active rows data into "master"

    hey arlu

    thxs for your wonderful help. i am using the macro in the "workbook" sheet as when the users go are on the P1, P2..P12 sheet, i would like the macro to automatically consolidate the data from the P1-P12 worksheets.

    the only issue using the macro you provided...it is taking a long time and also, it does not overwrite the existing data in the master sheet

    here is the code

    Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Dim i As Long
    Dim lrow As Long
    
    For i = 1 To Worksheets.Count
        With Worksheets(i)
            If .Name Like "P*" Then
                lrow = .Range("B" & .Rows.Count).End(xlUp).Row
                .Range("B10:P" & lrow).Copy Worksheets("Master").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
            End If
        End With
    Next i
    
    End Sub

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: macro to paste last active rows data into "master"

    Why are you using the workbook_sheetdeactivate event? You can use it as normal sub-routine as in Sub copy_data(). This could be slowing down your code.

    To clear the contents of the master sheet, use this updated code
    Private Sub copy_data()
    Dim i As Long
    Dim lrow As Long
    
    worksheets("Master").cells.clearcontents
    for i = 1 to worksheets.count
       if worksheets(i).name <>"Master" then
           worksheets(i).rows("10:10").copy worksheets("Master").range("A10")
       end if
    next i
    
    For i = 1 To Worksheets.Count
        With Worksheets(i)
            If .Name Like "P*" Then
                lrow = .Range("B" & .Rows.Count).End(xlUp).Row
                .Range("B10:P" & lrow).Copy Worksheets("Master").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
            End If
        End With
    Next i
    
    End Sub

  12. #12
    Forum Contributor
    Join Date
    11-26-2010
    Location
    usa
    MS-Off Ver
    Office 365
    Posts
    1,233

    Re: macro to paste last active rows data into "master"

    hey arlu

    the sole reason why i was using workbook_event was to make the macro automated.

    ideally i do not want the user to run any macros. i would like if.when they click on another worksheet, the data gets consolidated into the "master" sheet. does that make sense...will the updated macro you provided perform that task?

    thx u so much for your help, it is really appreciated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1