Macro to Select File when opening

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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
 
Upvote 0
Thanks for the code.

Where do I include this with my code ?
 
Upvote 0
Replace this line:
VBA Code:
A = Application.GetOpenFilename

And replace exit function with exit sub.
 
Upvote 0
Solution
Many Thanks for the help. Code works perfectly
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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