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!
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Fazza

MrExcel MVP
Joined
May 17, 2006
Messages
9,368
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,878
Messages
5,598,623
Members
414,248
Latest member
Tirali

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
Top