can you correct my code for opening and copying data from one workbook to another please

Eurekaonide

Active Member
Joined
Feb 1, 2010
Messages
422
Hi All

can you tell me where I am going wrong here please.

I am trying to open another workbook select the data in the only worksheet it will have between Columns A:BG and as far down as the data goes in column A and copy it into the workbook I am running the code from into the worksheet "Changes".
I cant use the worksheet or workbook name that I'm opening as it could be called something new each time, however the columns are always the same the rows will differ.
Its opening but its not selecting and copying the data across into this workbook sheet "changes". Please can you help.

VBA Code:
Sub OpenFileDialogBox()
On Error Resume Next
    
    
    Dim FilePath As String
    
    FilePath = Application.GetOpenFilename()
    
    Workbooks.Open (FilePath)
      
    ActiveWorkbook.Worksheets.Select
    Range("A:BG" & Rows.Count, "A").End(xlUp).Select
    Selection.Copy
    
    ThisWorkbook.Activate
    Sheets("Changes").Select
    Range("A1").PasteSpecial
    
      
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi. Give the below a try:

VBA Code:
Sub OpenFileDialogBox()

Dim tWB As Workbook: Set tWB = ThisWorkbook
Dim oFile As String: oFile = Application.GetOpenFilename()
Dim xWB As Workbook: Set xWB = Workbooks.Open(oFile)

With xWB.Sheets(1)
    .Range("A1:BG" & .Cells(.Rows.Count, 1).End(xlUp).Row).Copy
End With

With tWB.Sheets("Changes")
    .Activate
    .Range("A1").PasteSpecial
End With

'uncommment the below to close the external workbook
'xWB.Close savechanges:=False
      
End Sub
 
Upvote 0
Solution
Or
Code:
Sub Copy_From_Closed_WB()
With Workbooks.Open(Application.GetOpenFilename)
    .Sheets(1).Cells(1).Resize(.Sheets(1).Columns(1).SpecialCells(2).Rows.Count, .Sheets(1).UsedRange.Columns.Count).Copy ThisWorkbook.Sheets("Changes").Cells(1)
    .Close False
End With
End Sub
 
Upvote 0
Hi. Give the below a try:

VBA Code:
Sub OpenFileDialogBox()

Dim tWB As Workbook: Set tWB = ThisWorkbook
Dim oFile As String: oFile = Application.GetOpenFilename()
Dim xWB As Workbook: Set xWB = Workbooks.Open(oFile)

With xWB.Sheets(1)
    .Range("A1:BG" & .Cells(.Rows.Count, 1).End(xlUp).Row).Copy
End With

With tWB.Sheets("Changes")
    .Activate
    .Range("A1").PasteSpecial
End With

'uncommment the below to close the external workbook
'xWB.Close savechanges:=False
     
End Sub
Thank you :)
 
Upvote 0
Or
Code:
Sub Copy_From_Closed_WB()
With Workbooks.Open(Application.GetOpenFilename)
    .Sheets(1).Cells(1).Resize(.Sheets(1).Columns(1).SpecialCells(2).Rows.Count, .Sheets(1).UsedRange.Columns.Count).Copy ThisWorkbook.Sheets("Changes").Cells(1)
    .Close False
End With
End Sub
Thanks You :)
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,974
Members
448,934
Latest member
audette89

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