Excel VBA - Open workbook, if not already opened, using wildcard or partial name match

TropicalMagic

New Member
Joined
Jun 19, 2021
Messages
47
Office Version
  1. 365
Platform
  1. Windows
I would like to open a workbook, if it is not already opened, using a wildcard or partial match of its name.

The only constant name components in the workbook’s name are “Report Due”.



Here are the codes I have tried so far:

```

On Error Resume Next

Dim MyWorkbook As Workbook

Set MyWorkbook = Workbooks("Report Due*.xlsx")

If Err <> 0 Then

On Error GoTo 0

Workbooks.Open ("Report Due*.xlsx")

End If



```

AND


```



Dim MyFileName As Variant

Dim myFolderPath As String



myFolderPath = Application.DefaultFilePath

MyFileName = Dir(myFolderPath & "Report Due*.xlsx")



If MyFileName <> "" Then

Workbooks.Open (myFolderPath & “\” & MyFileName)

End If



```

Both sequences of codes don’t seem to be working as there is an Error 1004: Sorry, we couldn’t find “Report Due*.xlsx”


Many thanks in advance!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You must loop on the Workbooks collection to compare each name via the Like operator in order to check if the workbook is yet opened …​
For the Dir function it seems you forgot to add a backslash before the name …​
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,607
Members
449,090
Latest member
vivek chauhan

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