Acquiring A Workbook Name From Several Open Excel Workbooks - Matching A Filename

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have an enterprise application that generates an Excel Workbook. It automatically creates it's own unique name for each report it generates. The workbook name always starts with "Active_Report_" and then is completed with a random set of numbers followed by ".xlsx"

I have an Excel VBA based application that needs to reference that particular workbook created at that time. Since the workbook always has a different name, I can't define it in code (easily) as it's dynamic.

Is there a way that I can assess the names of any open workbooks, and seek out the open one with a name "Active_Report * .xlsx" and set that workbook name to the variable declared as a workbook? (Note: not all files generated by this enterprise application creates this format of a filename. The only thing each and every file name has in common is "Active_Report" and ".xlsx")

Eg. using pseudo-code

enterprise creates and opens for viewing Excel workbook: "Active_Report-2023-1103T1104100.321.xlsx"

Code:
Public wb_data as worbook
Dim wbname as string
wbname = full name of the workbook identified as "Active_Report ??????.xlsx" from among the open workbooks
Set wb_data = workbook(wbname)
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I think I found a solution ...
Code:
Private Sub transfer_raw()
    Dim wbnum As Long
    Dim wb As Workbook
    Dim wb_data as workbook
    wbnum = 0
    'how many workbooks are open
    For Each wb In Workbooks
        wbnum = wbnum + 1
    Next wb
    
    If wbnum < 2 Then
        'there aren't enough open workbooks in which a valid one would exist
        MsgBox "There are no open Excel Workbooks. (besides this one)"
    Else
        MsgBox "There are " & wbnum & " open worksheets."
        For Each wb In Workbooks
            If wb.Name Like "active_report*" Then
                'one of the open workbooks matches the name "active_report"
                MsgBox wb.Name & " found."
                Set wb_data = workbooks(wb.name)
                Exit For
            End If
        Next wb
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,879
Messages
6,175,142
Members
452,615
Latest member
bogeys2birdies

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