Copy Select Cells from multiple worksheets,

DPeter2101

New Member
Joined
Dec 15, 2004
Messages
11
Hello,

I am not very good with coding but have managed to use what I find and tweak it to fit my needs quite often. However, in this case I can't seem to get what I need and can't find anything that will work for me. I would appreciate any assistance I can get.

I have a work book with multiple Worksheet. These Worksheet have data I would like to move to one Worksheet. The data is in the same places on every Worksheet but I do not need the whole row. I Just need selected cells throughout the Worksheet. These are the Cells I need to copy ("C2", "F2", "B5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14").

I found this code and adjusted it to copy the data from the Cells that I needed. However it will only copy from one Worksheet and not multiple.
Code:
Sub test()
Dim LR As Long, i As Long, cls
cls = Array("C2", "F2", "B5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14")
With Sheets("Sheet2")
    LR = WorksheetFunction.Max(6, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet1").Range(cls(i)).Value
    Next i
End With
End Sub

I also found this code that copies from multiple worksheets but it copies ranges and not selected cells. It also deletes the master sheet each time it runs.  I have been trying to use part of both codes to get what I need but have failed miserably.. 
 
Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
    On Error GoTo 0
End Function

Function LastCol(sh As Worksheet)
    On Error Resume Next
    LastCol = sh.Cells.Find(What:="*", _
                            After:=sh.Range("A1"), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
    On Error GoTo 0
End Function
Sub CopyRangeFromMultiWorksheets()
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim CopyRng As Range
    Dim LR As Long, i As Long, cls

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

    'Delete the summary sheet if it exists.
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True

   '  Add a new summary worksheet.
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "RDBMergeSheet"

    'Loop through all worksheets and copy the data to the
    'summary worksheet.
    For Each sh In ActiveWorkbook.Worksheets
        If sh.Name <> DestSh.Name 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("A1").CurrentRegion



            ' 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
            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

To sum up what I’m looking to do.
1. Copy select cells from multiple worksheets to a Master Worksheet.
2. Have the code find the last unused row and start adding new data at that location
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Could I please get some assistance with this. I've managed to get it to work. However, it's a mess. I have to have the exact number of sheets to get this code to work. I need it to loop through however many pages the Workbook contains. (I'm trying to learn but this one is killing me.)

Code:
Private Sub Headers()
Dim LR As Long, i As Long, cls

cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")

    LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet2").Range(cls(i)).Value
    Next i
    
End With

cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")

    LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet3").Range(cls(i)).Value
    Next i
    
End With

cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")

    LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet4").Range(cls(i)).Value
    Next i
    
End With

cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")

    LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet5").Range(cls(i)).Value
    Next i
    
End With
cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")

    LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet6").Range(cls(i)).Value
    Next i
    
End With

cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")

    LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet7").Range(cls(i)).Value
    Next i
    
End With

cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")
 
   LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet8").Range(cls(i)).Value
    Next i
    
End With

cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")
  
  LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet9").Range(cls(i)).Value
    Next i
    
End With

cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")

    LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet10").Range(cls(i)).Value
    Next i
    
End With

cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")
   
    LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet11").Range(cls(i)).Value
    Next i
    
End With
cls = Array("C2", "F2", "B5", "C5", "B6", "B7", "B8", "B9", "F4", "F6", "F8", "C14", "C16", "F14", "C16", "F16", "C21", "F21", "C23", "F23", "C27", "F27", "C28", "C29", "F29", "B31", "B32", "B33", "B34", "B35", "B36", "B37", "B38", "B39", "B40", "B41", "B42", "B43")
With Sheets("Master")

    LR = WorksheetFunction.Max(2, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Sheets("Sheet12").Range(cls(i)).Value
    Next i
    
End With

End Sub
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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