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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
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,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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