VBA Reference Unsaved Files

kaylan

New Member
Joined
Aug 19, 2019
Messages
9
Hello,

This is probably a quick fix but for the life of me I cannot figure it out.

I have a project that references an open unsaved file ("Invoice Data.xlsx"). Every day a new file is emailed to us with invoicing information. The macro file imports the relevant data and runs some calculations. The problem is that sometimes Excel will add a number to the end of the file name, I believe from opening multiple "Invoice Data.xlsx" files in a short period of time. For example, the first file will open normally but then others will open as "Invoice Data (003).xlsx" instead.

This does not work with the macro, since it is coded to specifically draw from "Invoice Data.xlsx". Is there a way to code the macro to only look for "Invoice Data" and ignore the end of the file name?

Thank you in advance!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
hello, kaylan. please consider code below. regards, Fazza
VBA Code:
Sub Maybe()

    Dim wbkInvoiceData As Excel.Workbook
        
    Set wbkInvoiceData = FindWorkbook(sWithNameLike:="Invoice Data")

    If wbkInvoiceData Is Nothing Then
        MsgBox Prompt:="didn't find it", Buttons:=vbExclamation, Title:="file not found"
    Else
        MsgBox Prompt:=wbkInvoiceData.Name, Buttons:=vbInformation, Title:="file found"
    End If

End Sub

Private Function FindWorkbook(ByVal sWithNameLike As String) As Excel.Workbook
    
    Dim wbkLoop As Excel.Workbook
        
    For Each wbkLoop In Application.Workbooks
        If InStr(wbkLoop.Name, sWithNameLike) > 0 Then
            Set FindWorkbook = wbkLoop
            Exit For
        End If
    Next wbkLoop
    Set wbkLoop = Nothing

End Function
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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