Macro to Open File using Wilcard Character

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I hsve tried to write code to open a file in C:\Sales Reports by month" using a wilcard characterso to make it easier to select a file to open and copy the data for eg BR1 Sales Report Oct 2022.xlsm

I get a run time error 10 "This array is fixed or temorarily locked" and the code below is highlighted


Code:
  A = .SelectedItems(1)

See full code below

It would be appreciated if someone could amend my code


Code:
 Sub Open_Workbook()
ChDir ("C:\Sales Reports by month")
A:
Dim A     As Variant
Dim LR As Long
     A = Application.GetOpenFilename(MultiSelect:=True)
    If TypeName(A) = "Boolean" Then Exit Sub
    
    Dim file As Variant
    
    Application.ScreenUpdating = False
   For Each file In A
        With Workbooks.Open(file)
 With Application.FileDialog(msoFileDialogFilePicker)
        .ButtonName = "Open"
        With .Filters
            .Clear
            .Add "Excel Files", "*.xlsm"
        End With
        .InitialFileName = "*BR1*.xlsm"
        .Title = "Select File"
         If .Show <> -1 Then Exit Sub
        A = .SelectedItems(1)
    End With
     
       With Sheets(2)
            .UsedRange.Copy _
                Destination:=ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1)
                  End With
           .Close SaveChanges:=False
         
        End With
   
    
 Next

 Application.ScreenUpdating = True
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I'm not quite sure why you have 2 times the instruction to open files, but try the following:

VBA Code:
Sub Open_Workbook()
  Dim xfile As Variant
  Dim wb2 As Workbook
  
  Application.ScreenUpdating = False
  
  ChDir ("C:\Sales Reports by month")
  With Application.FileDialog(msoFileDialogFilePicker)
    .ButtonName = "Open"
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xlsm"
    .InitialFileName = "*BR1*.xlsm"
    .Title = "Select File"
    If .Show <> -1 Then Exit Sub
    For Each xfile In .SelectedItems
      Set wb2 = Workbooks.Open(xfile)
      wb2.Sheets(2).UsedRange.Copy _
        ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1)
      wb2.Close False
    Next
  End With
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,261
Messages
6,123,931
Members
449,134
Latest member
NickWBA

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