Macro to copy from different (dynamic) workbooks

igeron

New Member
Joined
Oct 25, 2010
Messages
11
Hi All,

I have an excel template which have to be filled with the data from another (Last year's) workbook. I have a macro which copy data from the excel sheet, only if I state the name and adress of the last year's excel file within the macro.

My question is;
can I create a macro so that in the beggining of the macro run, it will ask me the resource/last year file and I can browse it. Then the macro will pick all the data from the chousen file.

Thanks
Igal
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try like this

Code:
Sub SelectFile()
Dim sFile As Variant
sFile = Application.GetOpenFilename("Files (*.xls),*.xls", , "Select A File")
If sFile <> "False" Then Workbooks.Open (sFile)
End Sub
 
Upvote 0
Thank you very much for the answer,

However I managed to fail on the copying part as well :oops:

The code should be as follow, but failing

Code:
Sub LYDATA()
Application.ScreenUpdating = False
 
Dim sFile As Variant, wbs As Workbook, wbc As Workbook
Set wbc = ActiveWorkbook
sFile = Application.GetOpenFilename("Files (*.xls),*.xls", , "Select A File")
If sFile = "False" Then Exit Sub
Set wbs = Workbooks.Open(sFile)
 
wbc.Worksheets("sheet1").Range("A1") = wbs.Worksheets("sheet2").Range("c2").value

Application.ScreenUpdating = True
        
End Sub

Thanks
Igal
 
Upvote 0
I changed the macro into this:

However When I run, I receive Run-time error '9': Subscript out of range


Code:
Sub LYSDATA1() 
Dim sFile As Variant, wb As Workbook
sFile = Application.GetOpenFilename("Files (*.xls),*.xls", , "Select A File")
If sFile = "False" Then Exit Sub
Set wb = Workbooks.Open(sFile)
wb.Worksheets("Sheet1").Range("A1:A10").Copy Destination:=ThisWorkbook.Worksheets("DATA").Range("A1")
wb.Close savechanges:=False 
'here is the error
 
End Sub
 
Upvote 0
on this one, sorry


Code:
wb.Worksheets("Sheet1").Range("A1:A10").Copy Destination:=ThisWorkbook.Worksheets("DATA").Range("A1")
 
Upvote 0
That means that either or both the workbook that you opened doesn't have a Sheet1 or the workbook with the code doesn't have a sheet DATA.
 
Upvote 0
Actually I have both

This did the trick

Code:
Sub LYSDATA1()
Application.ScreenUpdating = False
 
Dim sFile As Variant, wb As Workbook, wbc As Workbook
Set wbc = Application.ActiveWorkbook
sFile = Application.GetOpenFilename("Files (*.xls),*.xls", , "Select A File")
If sFile = "False" Then Exit Sub
Set wb = Workbooks.Open(sFile)
wb.Sheets("Sheet1").Select
Range("A1:A10").Copy
wbc.Activate
Sheets("Data").Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False
wb.Close savechanges:=False
wbc.Activate
Application.ScreenUpdating = True
End Sub


Thanks :)
Igal
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

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