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
It would be appreciated if someone could amend my code
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