Macro to Select File when opening

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,996
Office Version
  1. 2019
Platform
  1. Windows
I have the following macro that opens a file directory C:\extract and allows the user to select a file within this director and to copy and paste the data

The macro works perfectly, but I need it amended to allow the user to select a .xlsx file containing the name "Statement" eg XTRM Ltd Statement.xlsx


Your assistance in this regard is most appreciated

Code:
 Sub Open_Workbook()

Dim LR As Long

 '
      
        Application.ScreenUpdating = False
     Application.EnableEvents = False
     Application.Calculation = xlCalculationManual
    Dim nb As Workbook, ts As Worksheet, A As Variant
    Dim rngDestination As Range
  
    
    Set ts = ActiveSheet
    
    On Error Resume Next
    Set rngDestination = ts.[A1]
    On Error GoTo 0
    If rngDestination Is Nothing Then Exit Sub
    ChDir ("C:\extract")
MsgBox "Select Statement file applicable for this Branch"
    A = Application.GetOpenFilename
    If A = False Or IsEmpty(A) Then Exit Sub
    
    Application.ScreenUpdating = False
    
    Set nb = Workbooks.Open(A)
    ThisWorkbook.Activate
  
    
    
   nb.Sheets(1).Range("A1:Z2000").Copy
  
       rngDestination.PasteSpecial Paste:=xlPasteValues
    rngDestination.PasteSpecial Paste:=xlPasteFormats
    Application.CutCopyMode = False
       
    
    nb.Close savechanges:=False

    
         Application.ScreenUpdating = True
         
     Application.EnableEvents = True
     Application.Calculation = xlCalculationAutomatic
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,854
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
the GetOpenFilename method can accept filters, but unfortunately the dialog box filters are limited to file extensions.
You can try this approach:
VBA Code:
with Application.FileDialog(msoFileDialogFilePicker)
.InitialFileName ="*Statement*.xlsx"
If .Show = 0 Then Exit Function
A = .SelectedItems(1)
end with
 

howard

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

Where do I include this with my code ?
 

bobsan42

Well-known Member
Joined
Jul 14, 2010
Messages
1,854
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Replace this line:
VBA Code:
A = Application.GetOpenFilename

And replace exit function with exit sub.
 
Solution

howard

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

Forum statistics

Threads
1,143,836
Messages
5,721,082
Members
422,339
Latest member
SHIVATVM

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