Open Workbooks and Copy Data to after last row containing data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
Platform
  1. Windows
I have tried to write code to open a workbook (have several that needs to be opened one at a time) and to copy the data from Col Q2:R to sheet1 Col A in the destination workbook. Once the second workbook is opened, it must copy the data to the first blank row after the last row containing data

I have the following code, but comes up with subscript out of range and the code "Sheets("Sheet1").Select" is highlighted

Your assistance in resolving this is most appreciated



Code:
 Sub Open_Workbook()
ChDir ("C:\my Documents")
Dim Lr As Long

Lr = Cells(Rows.Count, "A").End(xlUp).Row



Dim nb As Workbook, tw As Workbook, ts As Worksheet
A = Application.GetOpenFilename
If A = False Or IsEmpty(A) Then Exit Sub
With Application
    .ScreenUpdating = False
    End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set nb = Workbooks.Open(A)

With Sheets("Imported Data")
Range("Q2:R" & Lr).Copy

Sheets("Sheet1").Select
    Range("A1").Select
Do Until IsEmpty(ActiveCell)
         ActiveCell.Offset(1, 0).Select
      Loop
    
    Selection.Paste
    
    End With
End Sub
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,378
Try this...

Code:
[color=darkblue]Sub[/color] Open_Workbook()
    
    [color=darkblue]Dim[/color] A     [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    
    ChDir "C:\My Documents"
    A = Application.GetOpenFilename
    [color=darkblue]If[/color] A = [color=darkblue]False[/color] [color=darkblue]Or[/color] IsEmpty(A) [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    
    Application.ScreenUpdating = [color=darkblue]False[/color]
    
    [color=darkblue]With[/color] Workbooks.Open(A)
        [color=darkblue]With[/color] .Sheets("Imported Data")
            .Range("Q2", .Range("R" & Rows.Count).End(xlUp)).Copy _
                Destination:=ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1)
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        .Close SaveChanges:=[color=darkblue]False[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    Application.ScreenUpdating = [color=darkblue]True[/color]
    
End [color=darkblue]Sub[/color]
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
Platform
  1. Windows
Thanks for the help, code works perfectly
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
Platform
  1. Windows
I have tried to amend your code to open a workbook and copy the following columns into sheet1 of the destination workbook, but M4 onwards dos not copy

Kindly amend the code below

A4 up to the last row containing data
J4 up to the last row containing data
L4 up to the last row containing data
M4 up to the last row containing data

Code:
 Sub Open_Workbook()
    
    Dim A     As Variant
    
    ChDir "C:\My Documents"
    A = Application.GetOpenFilename
    If A = False Or IsEmpty(A) Then Exit Sub
    
    Application.ScreenUpdating = False
    
    With Workbooks.Open(A)
        With .Sheets("Recon")
            .Range("A4", .Range("A" & Rows.Count).End(xlUp)).Copy _
                Destination:=ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1)
                                .Range("J4", .Range("J" & Rows.Count).End(xlUp)).Copy _
                Destination:=ThisWorkbook.Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Offset(1)
                   .Range("L4", .Range("L" & Rows.Count).End(xlUp)).Copy _
                Destination:=ThisWorkbook.Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Offset(1)
                 .Range("M4", .Range("M" & Rows.Count).End(xlUp)).Copy _
                Destination:=ThisWorkbook.Sheets("Sheet1").Range("E" & Rows.Count).End(xlUp).Offset(1)
                End With
                .Close SaveChanges:=False
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,378
It looks good to me. What exactly happens when you say it doesn't copy; error, wrong data copied, no data copied?
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,701
Office Version
  1. 2019
Platform
  1. Windows
My Aplologies, for getting you to check for nothing, The Data in the source file was in Cols A, J, K & L

I have now amended this in the source file to A, J, L & M and the code works perfectly.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,561
Messages
5,596,846
Members
414,107
Latest member
Tigretto

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
Top