VBA Help - Defining 2nd Open Excel Workbook without knowing File Name - Excel 2016

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. 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,

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:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You can do that quite easily, BUT what if the user has more than 2 workbooks open?
Will file B have a specific naming convention that could be used to identify it?
 
Upvote 0
The users have been instructed to only have two files open when running.

And Yes/No in regards to the name of the Downloaded Query, the file name is structured like so "Collections Report - xxxxxx" The x's represent random numbers the query generates when exporting.
 
Upvote 0
Ok, how about
Code:
Sub GetWb()
   Dim wbk As Workbook, Wbk2 As Workbook
   For Each wbk In Workbooks
      If LCase(wbk.Name) Like "collections report*" Then Set Wbk2 = wbk
   Next wbk
End Sub
 
Upvote 0
Woohoo!

That worked like a charm! Thanks for the help on this. I am sure I will be able to adapt this to other projects if they come up.

Here is your code parsed in with my original project.

Code:
Sub DownloadReport()


Dim wbk     As Workbook, Wbk2 As Workbook, wB1 As Workbook
Dim Wb2Sht  As Worksheet
   
For Each wbk In Workbooks
   If LCase(wbk.Name) Like "collections report*" Then Set Wbk2 = wbk
Next wbk
   
MsgBox Wbk2.Name


Set wB1 = ThisWorkbook
Set Wb2Sht = Wbk2.Worksheets(1)


Wbk2.Activate
Wb2Sht.Cells.Copy


wB1.Sheets("Report").Range("A1").PasteSpecial xlPasteFormats
wB1.Sheets("Report").Range("A1").PasteSpecial xlPasteValues


Wbk2.Close False
   
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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