Macro to Open File using Wildcard

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following code to open a file and to copy and paste the data

I need the code amended so as to use a wildcard to select a file which starts with eMrb and ends with .xlsx for eg eMrb_listEnquiry.xlsx

Your assistance in this regard is most appreciated



Code:
Sub Open_Workbook()

ChDir ("C:\downloads")

Dim nb As Workbook, ts As Worksheet, A As Variant

Dim rngDestination As Range

Sheets("Imported Data").Select

With Range("a1:V2000")

.ClearContents

End With


Set ts = Sheets("Imported Data")



On Error Resume Next

Set rngDestination = ts.[a1]

On Error GoTo 0

If rngDestination Is Nothing Then Exit Sub

ChDir ("C:\extract")

MsgBox "Select Enquiry File"



A = Application.GetOpenFilename

If A = False Or IsEmpty(A) Then Exit Sub

Application.ScreenUpdating = False



Set nb = Workbooks.Open(Filename:=A, local:=True)

ThisWorkbook.Activate



nb.Sheets(1).Range("A1:V2000").Copy

rngDestination.PasteSpecial Paste:=xlPasteValues

rngDestination.PasteSpecial Paste:=xlPasteFormats

Application.CutCopyMode = False

nb.Close savechanges:=False

With Sheets("Imported Data")

.Range("A1:A4").EntireRow.Delete

End With

ChDir "C:\My documents"

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try replacing:
VBA Code:
A = Application.GetOpenFilename

If A = False Or IsEmpty(A) Then Exit Sub
with:
VBA Code:
    A = Dir("C:\extract\eMrb*.xlsx")
    If A = vbNullString Then Exit Sub
    A = "C:\extract\" & A
 
Upvote 0
Solution

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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