Copy data from workbook to another workbook

Demirion

Board Regular
Joined
Sep 21, 2022
Messages
66
Platform
  1. Windows
Hello, I need to copy data from one multi-sheet workbook to another multi-sheet workbook. I entered the names of the sheets permanently. As for the data from the sheets, they can change every month, so I can't copy a fixed range. I need to use lRow and lCol for each sheet but I have no idea how to do it. Here is some example code which is not working well, it is showing errors because lRow and lCol are working wrong. Maybe there is a more efficient solution? Maybe a better solution would be to do it without variables?
VBA Code:
Dim Workbook1 As Workbook
Dim lRow As Long
Dim lCol As Long

lRow = Cells(Rows.Count, 1).End.(xlUp).Row
lCol = Cells(1, Columns.Count).End.(xlToLeft).Column

With Workbook1
  With .Sheets("A")
  .Range(.Cells(2, 1), .Cells(lRow, lCol)).Copy
  ThisWorkbook.Sheets("Ark1").Range("A2").PasteSpecial xlPasteValues
End With

With .Sheets("B")
  .Range(.Cells(2, 1), .Cells(lRow, lCol)).Copy
  ThisWorkbook.Sheets("Ark2").Range("A2").PasteSpecial xlPasteValues
 End With
End With
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
You have not defined a workbook for the Workbook1 variable, and you have some syntax errors.
A suggestion (not tested):
VBA Code:
    Dim Workbook1 As Workbook
    Dim lRow As Long
    Dim lCol As Long
    
    Set Workbook1 = Application.Workbooks("MyData.xlsx")    '<- you must define this
    
    With Workbook1
        With .Worksheets("A")
            lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
            lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            .Range(.Cells(2, 1), .Cells(lRow, lCol)).Copy
            ThisWorkbook.Sheets("Ark1").Range("A2").PasteSpecial xlPasteValues
        End With
        
        With .Worksheets("B")
            lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
            lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
            .Range(.Cells(2, 1), .Cells(lRow, lCol)).Copy
            ThisWorkbook.Sheets("Ark2").Range("A2").PasteSpecial xlPasteValues
        End With
    End With
 
Upvote 0
Solution
This is just sample code. I have used this solution before but thanks for your help anyway.
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,072
Members
449,093
Latest member
ripvw

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