Set Range of specific columns with different used ranges in multiple sheets

ddander54

Board Regular
Joined
Oct 18, 2012
Messages
97
I'm trying to modify Ron de Bruin's CopyRangeFromMultiWorksheets() macro to copy individual columns from multiple worksheets, so I modified the code with

Code:
Set CopyRng = sh.Range("B1:B11, L1:L11, K1:K11, M1:M11")
as a test. That worked great, except each worksheet has a different number of rows.

So what I need is something like this because the last used cell in each worksheet is different, but of course the code below is causing an error on compile. The columns in each worksheet are the same, just different number of rows in each worksheet, so I'm trying to use something like this to determine the last used cell in each worksheet. Theoretically the row count of Column B is the same as L, K, M in each worksheet, so if there is a better way to do this than row counting each column that would be great.

psuedocode:
Code:
Set CopyRng = sh.Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row),& _
                                    "L1:L" & Range("L" & Rows.Count).End(xlUp).Row),& _
                                    "K1:K" & Range("K" & Rows.Count).End(xlUp).Row),& _
                                    "M1:M" & Range("M" & Rows.Count).End(xlUp).Row)

Thanks,
Don
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Don

The comma should be within the quotes and you have some extra ), try this.
Code:
Set copyrng = sh.Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row & _
                                    ",L1:L" & Range("L" & Rows.Count).End(xlUp).Row & _
                                    ",K1:K" & Range("K" & Rows.Count).End(xlUp).Row & _
                                    ",M1:M" & Range("M" & Rows.Count).End(xlUp).Row)
 
Upvote 0
Norie,

Thanks for the response. Your suggestion fixed the code since the syntax is now correct, but now the code is failing further down the script.

Here is the whole macro:
Code:
Sub CopyRangeFromMultiWorksheets()
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim CopyRng As Range
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    ' Delete the summary sheet if it exists.
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("MergeSheet").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    ' Add a new summary worksheet.
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "MergeSheet"
    ' Loop through all worksheets and copy the data to the
    ' summary worksheet.
    For Each sh In ActiveWorkbook.Worksheets
        'If sh.Name <> DestSh.Name Then
        If IsError(Application.Match(sh.Name, _
        Array(DestSh.Name, "Run Day Calendar", "RD Breakdown", "Summary - NYL", "Revised Plan - TCs for NYL"), 0)) Then

            ' Find the last row with data on the summary worksheet.
            Last = LastRow(DestSh)
            ' Specify the range to place the data.
            'Set CopyRng = sh.Range("B1:B100, L1:L100, K1:K100, M1:M100, AR1:AR100, AS1:AS100")
            Set CopyRng = sh.Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row & _
                                    ",L1:L" & Range("L" & Rows.Count).End(xlUp).Row & _
                                    ",K1:K" & Range("K" & Rows.Count).End(xlUp).Row & _
                                    ",M1:M" & Range("M" & Rows.Count).End(xlUp).Row)

            ' Test to see whether there are enough rows in the summary
            ' worksheet to copy all the data.
            If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
                MsgBox "There are not enough rows in the " & _
                   "summary worksheet to place the data."
                GoTo ExitTheSub
            End If
            ' This statement copies values and formats from each
            ' worksheet.
            CopyRng.Copy    ' >>>>>>>>>>>>>>> Failing here now <<<<<<<<<<<<
            With DestSh.Cells(Last + 1, "A")
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
            End With
            ' Optional: This statement will copy the sheet
            ' name in the H column.
            DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name
        End If
    Next
ExitTheSub:
    Application.Goto DestSh.Cells(1)
    ' AutoFit the column width in the summary sheet.
    DestSh.Columns.AutoFit
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

Thanks,
Don
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top