Macro to select Files and Copy Data to appropriate sheets

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have VBA code to allow user to select several files containing Sept in C:\my documents and limit this to contain BR and sept in the name. The files selected are to be copied to the sheet name that is the same as the file name, excluding .xls . For Eg if the file name is BR1Sept.xls , then Col A:D from row 1 is be copied to sheet BR1sept, the second file eg BR2SothSept.xls to be copied on sheet BR2Sotsept.



I get a run time error "Invalid procedure or Argument and this code is causing the issue

Code:
 .Filters.Add "Excel Files", "BR1*Sept*.xls*"


Code:
Sub SelectAndCopyFiles()

 
    Dim MyFolder As String
    Dim MyFile As String
    Dim MyPath As String
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim i As Integer
    
    'Open file picker dialog to select files
    With Application.FileDialog(msoFileDialogOpen)
        .Filters.Clear
        .Filters.Add "Excel Files", "BR*Sept*.xls*"
        .InitialFileName = "C:\my documents\"
        .Title = "Select BRSept Files"
        .AllowMultiSelect = True
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub
        'Loop through selected files
        For i = 1 To .SelectedItems.Count
            MyFile = .SelectedItems(i)
            'Extract sheet name from file name
            Set wb = Workbooks.Open(MyFile)
            Set ws = wb.Sheets(Left(Replace(MyFile, ".xls", ""), Len(Replace(MyFile, ".xls", "")) - 4))
            MyPath = ThisWorkbook.path & "\"
            'Copy data from selected range to appropriate sheet
            With ws
                .Range("A:D").Copy
                ThisWorkbook.Sheets(.Name).Range("A1").PasteSpecial xlPasteValues
            End With
            wb.Close False
        Next i
    End With
    Application.CutCopyMode = False
End Sub

It would be appreciated if someone could amend my code
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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