VBA copy data from multiple worksheets and paste in a specific, preformatted sheet.

Pettel

New Member
Joined
Jan 23, 2020
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,
I'm trying to gather information from multiple worksheets into a single preformatted table.
I've started with a dialog folder picker and I know the range that i need to copy from each sheet,but how do I paste what I've copied into a particular range and still have it offset.
For example I copy range "D4:R4" from "sheet1" and Paste it into "E5:S5" of "Sheet1_master", then I open another file and copy range "D4:R4" from "sheet1" but now i need to paste it to "E6:S6" of "Sheet1_master".

This is what I have so far:
Code:
Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
    xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
    xFileName = Dir(xFdItem & "*.xls*")
    Do While xFileName <> ""
    With Workbooks.Open(xFdItem & xFileName)
     Sheets("Scotopic A").Select
     Range("D4:R4").Copy Destination:=Sheets("Scotopic A_Master").Cells(Rows.Count, "E").End(xlUp).Offset(1, 0)
     End With
     xFileName = Dir
    Loop
    Application.ScreenUpdating = True
End Sub

How do I proceed from here?
 
Missing a period in front of Range
Code:
           With wb.Sheets("Scotopic A")
                    .Range("C4:R4").Copy Destination:=Workbooks("Master_ERG.xlsm").Sheets("Scotopic A_Master").Cells(Rows.Count, "E").End(xlUp).Offset(1, 0)
                    wb.Close False
            End With
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Because it's part of the With Statement, if you don't include the . then it will look at the active sheet
 
Upvote 0
ooo...it's like saying

wb.Sheets("Scotopic A").Range("C4:R4").Copy
 
Upvote 0
So now I want to duplicate this action and have the code look at the other worksheets and retrieve information from them.
But I get a "Loop without Do" error.

VBA Code:
Dim xFd As FileDialog, wb As Workbook
    Dim xFdItem As Variant
    Dim xFileName As String
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
        If xFd.Show = -1 Then
            xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
            xFileName = Dir(xFdItem & "*.xls*")
        End If
        Do While xFileName <> ""
        Set wb = Workbooks.Open(xFdItem & xFileName)
        With wb.Sheets("Scotopic A")
        .Range("C4:R4").Copy Destination:=Workbooks("Master_ERG.xlsm").Sheets("Scotopic A_Master").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
        wb.Close False
        With wb.Sheets("Scotopic B")
        .Range("C4:R4").Copy Destination:=Workbooks("Master_ERG.xlsm").Sheets("Scotopic B_Master ").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
        wb.Close False
                End With
            xFileName = Dir
        Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
It needs to be like
VBA Code:
      Do While xFileName <> ""
         Set wb = Workbooks.Open(xFdItem & xFileName)
         With wb.Sheets("Scotopic A")
            .Range("C4:R4").Copy Destination:=Workbooks("Master_ERG.xlsm").Sheets("Scotopic A_Master").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
         End With
         With wb.Sheets("Scotopic B")
            .Range("C4:R4").Copy Destination:=Workbooks("Master_ERG.xlsm").Sheets("Scotopic B_Master ").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
         End With
         wb.Close False
         xFileName = Dir
      Loop
 
Upvote 0
Each 'For' must have a 'Next', each 'With' must have an 'End With', each 'While' must have a 'Wend', each 'Do' must have a 'Loop' and each 'If" that uses more than one line must have an 'End If'. Where the 'End' part of the statement occurs in the code will determine if the code in between executes. True will execute all the code in between and False will prevent any of the code from executing. It is all based on logical process. But when one is missing, the VBA error message might indicate the worng one so all of the stanements have to be checked to see which is missing the 'End' statement.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,453
Members
448,898
Latest member
drewmorgan128

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