+ Reply to Thread
Results 1 to 14 of 14

Thread: copy range in worksheets paste to master

  1. #1
    Registered User
    Join Date
    01-31-2010
    Location
    Washington
    MS-Off Ver
    Excel 2007
    Posts
    5

    copy range in worksheets paste to master

    I am working with a workbook that i have created. The workbook has around thirty worksheets all with the starting nave of "DIV". I have a code that will loop throught the wrok book and copy the used range and insert them into a master. However what i need is a code that will loop through the wroksheets and only copy the used range only in columns A:P and starting in row 10 (i have headers from row 1-10). I have columns beyond "P" that has working information for that worksheet and do not want to copy it over to the master.

    Thank you for the help..

  2. #2
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: copy range in worksheets paste to master

    Hi,

    Use something like

    Sub CopyUsedRange
    Dim lUsedRows as Long, x as Long
    
       For x = 1 to Sheets.Count
          If Left(Sheets(x).Name,3) = "DIV"
             lusedRows = Sheets(x).Range("A10").CurrentRegion.Rows.Count
             Sheets(x).Range("A10:P" & lUsedRows).Copy Destination:= ' rest of your code here
          End If
        Next x
    
    End Sub
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  3. #3
    Registered User
    Join Date
    01-31-2010
    Location
    Washington
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: copy range in worksheets paste to master

    The code you gave me only seems to copy one row. I believe it could be because there is blank rows in the range. Is there a way for it to go all the way to the bottom and then com back up to the first cell with value. I cannot get rid of the blank rows, they must remain.

    Thank you

  4. #4
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: copy range in worksheets paste to master

    Hi,

    Yes, just substitute

    lUsedRows = Range(Sheets(x).Range("A10") , Sheets(x).Range("A"  & Rows.Count).End(xlUp)).Rows.Count
    Rgds
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  5. #5
    Registered User
    Join Date
    01-31-2010
    Location
    Washington
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: copy range in worksheets paste to master

    I am getting a run time error on that code line

  6. #6
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: copy range in worksheets paste to master

    Hi,

    Have you substituted it for the original line
       lusedRows = Sheets(x).Range("A10").CurrentRegion.Rows.Count
    and left all the other code in place?

    Rgds
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  7. #7
    Registered User
    Join Date
    01-31-2010
    Location
    Washington
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: copy range in worksheets paste to master

    I did change it out. here is the code i am working with
    Private Sub CommandButton1_Click()

    Dim DESTSH As Worksheet
    Dim Last As Long
    Dim lUsedRows As Long, x As Long

    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With

    ' delete existing rows in estimate sheet
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("ESUM").Select
    Rows("13:10000").Select
    Selection.DELETE Shift:=x1up
    On Error GoTo 0
    Application.DisplayAlerts = True

    Set DESTSH = ActiveWorkbook.Worksheets("ESUM")


    For x = 1 To Sheets.Count
    If Left(Sheets(x).Name, 3) = "DIV" Then
    lUsedRows = Range(Sheets(x).Range("A10"), Sheets(x).Range("A" & Rows.Count).End(xlUp)).Rows.Count
    Last = LASTROW(DESTSH)
    Sheets(x).Range("A10:P" & lUsedRows).Copy Destination:=DESTSH.Cells(Last + 1, 1)
    End If
    Next x
    ExitTheSub:
    Application.Goto DESTSH.Cells(1)
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    End Sub

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: copy range in worksheets paste to master

    Maybe this:
    lUsedRows = Range(Sheets(x).Range("A10"), Sheets(x).Range("A" & Rows.Count).End(xlUp)).Row
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    01-31-2010
    Location
    Washington
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: copy range in worksheets paste to master

    it made no difference.

  10. #10
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: copy range in worksheets paste to master

    Or:

    lUsedRows = Sheets(x).Range("A" & Rows.Count).End(xlUp).Row
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  11. #11
    Valued Forum Contributor
    Join Date
    02-27-2008
    Posts
    753

    Re: copy range in worksheets paste to master

    HI
    try these codes
    Sub vexcel()
    Dim a As Long, x As Long, y As Long
    Worksheets("ESUM").Cells.ClearContents
        For a = 1 To Sheets.Count
            If Worksheets(a).Name <> "ESUM" Then
            x = Worksheets(a).Range("A65536").End(xlUp).Row
            Worksheets(a).Range("A10:P" & x).Copy
            y = Worksheets("ESUM").Range("B65536").End(xlUp).Row
            Worksheets("ESUM").Range("A" & y + 2) = Worksheets(a).Name
            Worksheets("ESUM").Range("B" & y + 2).PasteSpecial
            End If
        Next a
    MsgBox "Complete"
    End Sub
    ravi

  12. #12
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: copy range in worksheets paste to master

    Ravi, resist the urge to hardcode a bottom row number like that. Each version of Excel has a different "bottom row" and hardcoding 65536 can have bad results on Excel 2007+ users.

    Use the Rows.Count syntax in place of the hardcoded number so Excel always starts from the actual bottom of the sheet, regardless of version.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  13. #13
    Valued Forum Contributor
    Join Date
    02-27-2008
    Posts
    753

    Re: copy range in worksheets paste to master

    Hi
    Thanks JB for your suggestion. I will do so hereafter.
    Ravi

  14. #14
    Registered User
    Join Date
    09-05-2010
    Location
    abu Dhabi
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: copy range in worksheets paste to master

    Hi,

    I have searched and searched in almost all the forums, but i have not been able to achieve what i need. I have to create a work sheet for my manager where he will assign different task/projects to us, and the same would be a updated in a shared file kept in our common DIR thus whenever we open we can view our individual sheets view the task assigned and update accordingly.

    Now, i have already created a Master sheet where task assigned gets populated on individual sheet but i am stuck at is how get the master sheet updated for column (Status) & column(update) when we update our sheets in column (F) & column (G) the same needs to be updated on master sheet ?

    Formula/macro anything is it possible?

    please
    Attached Files Attached Files

+ 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.2.0