Johnny Thunder
Well-known Member
- Joined
- Apr 9, 2010
- Messages
- 693
- Office Version
- 2016
- Platform
- MacOS
Hello All,
I have come across a project that I am not sure how to finish, here is the request details.
Have a macro in File A to copy details from a daily/weekly run query with a changing file name.
So essentially, Macro is in File A, and query is in File B. I need a way to Dim File B as a variable to run my code.
Here is where I am at,
I thought I could use the Activeworkbook but this is not working since the user would have to click on the query before triggering the macro and the users who will be using the file don't follow directions well. So I want to make this more fool proof.
Is there a way to maybe find all the excel sheets that are open and eliminate "ThisWorkbook" from the available options and Dim the remaining file as Wb2?
I have come across a project that I am not sure how to finish, here is the request details.
Have a macro in File A to copy details from a daily/weekly run query with a changing file name.
So essentially, Macro is in File A, and query is in File B. I need a way to Dim File B as a variable to run my code.
Here is where I am at,
Code:
Sub DownloadReport()
Dim Wb1 As Workbook, Wb2 As Workbook
Dim Wb2Sht As Worksheet
Set Wb1 = ThisWorkbook
Set Wb2 = ActiveWorkbook 'Not Working
Set Wb2Sht = Wb2.Worksheets(1)
Wb2.Activate
Wb2Sht.Cells.Copy
Wb1.Sheets("Report").Range("A1").PasteSpecial xlPasteFormats
Wb1.Sheets("Report").Range("A1").PasteSpecial xlPasteValues
Wb2.Close False
End Sub
I thought I could use the Activeworkbook but this is not working since the user would have to click on the query before triggering the macro and the users who will be using the file don't follow directions well. So I want to make this more fool proof.
Is there a way to maybe find all the excel sheets that are open and eliminate "ThisWorkbook" from the available options and Dim the remaining file as Wb2?
Last edited: