I'm new to VBA and need some help with my code. I am using Excel and Application.GetOpenFilename
to prompt users to select a file to open. As I copy data from one file to another, I am trying to Activate the workbook what was opened by the GetOpenFilename
application.
I am running VBA code from my Primary Workbook and here's what my code looks like.
Open import file
strGetFilename = Application.GetOpenFilename(, , "Open Import Workbook")
Workbooks.Open strGetFilename
For this example, let's assume that the file selected to open is Workbook2, I then select data from a table in Workbook2 and copy it.
ActiveSheet.ListObjects("table1").Range.Select
Selection.Copy
I now paste the data into my primary Workbook
Windows("PrimaryWorkbook.xlsm").Activate
Sheets("Sheet1").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Here is where I am getting tripped up. I want to go back to Workbook2 in order to copy other tables/sheets or to be able to perform other data edits and I don't want the file name to be hardcoded.
Any suggestions?