Copy one worksheet to another from different workbook

Jeffreyxx01

Board Regular
Joined
Oct 23, 2017
Messages
156
Hi guys,

Can someone help me to write a code that copy one worksheet from one workbook to another worksheet in another workbook.
I need to automate a few tasks at work and I cannot make complex macro.

Thanks for your support.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Could you be a bit more explicit? "still not working" doesn't give me much to go on.
 
Upvote 0
Ah sorry for that,

I received the error : Run-time error 424 Object Required ==> I press Debug and VBA highlight

Code:
.Range("A2:CF" & LastRow).Copy Sht.Range("A" & Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
You haven't declared, or set Sht
 
Upvote 0
Code:
Sub OpenFiles()

   Dim InitPth As String
   Dim Wbk As Workbook
   Dim Cnt As Long
   
   InitPth = "W:\Insights Team\ALL EFL\UK\Reporting\RawData"
   
   With Application.FileDialog(3)
      .Title = "Select the files"
      .AllowMultiSelect = True
      .InitialFileName = InitPth
      If .Show <> -1 Then Exit Sub
   
      For Cnt = 1 To .SelectedItems.Count
         Set Wbk = Workbooks.Open(.SelectedItems(Cnt))
         
         With Wbk.Sheets("Report")
      LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Range("A2:CF" & LastRow).Copy Sht.Range("A" & Rows.Count).End(xlUp).Offset(1)
      End With
            Application.DisplayAlerts = False
            Wbk.Close , False
      Next Cnt
   End With


End Sub
 
Upvote 0
Hi Fluff,
I have declared Sht, but I dont really see how to set Sht now.

Code:
Sub OpenFiles()

   Dim InitPth As String
   Dim Wbk As Workbook
   Dim Cnt As Long
[COLOR=#ff0000]   Dim Sht As Worksheet[/COLOR]
   
   InitPth = "W:\Insights Team\ALL EFL\UK\Reporting\RawData"
   
   With Application.FileDialog(3)
      .Title = "Select the files"
      .AllowMultiSelect = True
      .InitialFileName = InitPth
      If .Show <> -1 Then Exit Sub
   
      For Cnt = 1 To .SelectedItems.Count
    Set Wbk = Workbooks.Open(.SelectedItems(Cnt))
        With Wbk.Sheets("Report")
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            .Range("A2:CF" & LastRow).Copy Sht.Range("A" & Rows.Count).End(xlUp).Offset(1)
        End With
            Application.DisplayAlerts = False
            Wbk.Close , False
      Next Cnt
   End With


End Sub
 
Upvote 0
Hi Fluff,

I have set the Sht now however nothing is being copied from the sheets selected.
Do you have an idea why?

Code:
Sub OpenFiles()

   Dim InitPth As String
   Dim Wbk As Workbook
   Dim Cnt As Long
   Dim Sht As Worksheet
   
   InitPth = "W:\Insights Team\ALL EFL\UK\Reporting\RawData"
   
   With Application.FileDialog(3)
      .Title = "Select the files"
      .AllowMultiSelect = True
      .InitialFileName = InitPth
      If .Show <> -1 Then Exit Sub
   
      For Cnt = 1 To .SelectedItems.Count
    Set Wbk = Workbooks.Open(.SelectedItems(Cnt))
    Set Sht = ActiveWorkbook.Sheets("Report")
        With Wbk.Sheets("Report")
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            Sht.Range("A2:CF" & LastRow).Copy Sht.Range("A" & Rows.Count).End(xlUp).Offset(1)
        End With
            Application.DisplayAlerts = False
            Wbk.Close , False
      Next Cnt
   End With


End Sub
 
Upvote 0
You need to move this
Code:
Set Sht = ActiveWorkbook.Sheets("Report")
above where you open the new workbook.

When you open a workbook that book becomes the active book.
Alternatively if the sheet you are copying to is in the same book as the macro, change ActiveWorkbook to ThisWorkbook
 
Upvote 0
I have tried to change, it does not actually work when I move above,
Also I want the macro to copy the data starting from the cell A3 in to the new workbook "clean"

Code:
Set Wbk = Workbooks.Open(.SelectedItems(Cnt))
Set Sht = ThisWorkbook.Sheets("Report")

Set Sht = ThisWorkbook.Sheets("Report")
Set Wbk = Workbooks.Open(.SelectedItems(Cnt))
 
Upvote 0
In post#87 you've added sht in front of the copy from sheet, so you are now copying from/to the same sheet
 
Upvote 0

Forum statistics

Threads
1,215,241
Messages
6,123,823
Members
449,127
Latest member
Cyko

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