Macro to Open File using Wildcard

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,996
Office Version
  1. 2019
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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,032
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
 
Solution

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,996
Office Version
  1. 2019
Platform
  1. Windows
Thanks for the Help-This works perfectly
 

Forum statistics

Threads
1,143,687
Messages
5,720,295
Members
422,275
Latest member
Maria95

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