I have code below that browses to C:\My documents and allows a user to select a workbook. The Data copies D2 from last sheet and pastes this in A1 on sheet "imported Data" It thencopies AB2 to AD10 and pastes this in A2 onwards on sheet "Imported Data"
I need my code amended to limit the browsing to Vat.*.xlsm within C:\My Documents, which my code is not doing
Kindly amend my code accordingly
I need my code amended to limit the browsing to Vat.*.xlsm within C:\My Documents, which my code is not doing
Kindly amend my code accordingly
Code:
Sub ImportData()
Dim SourceFolder As String
Dim SourceFile As String
Dim ws As Worksheet
Dim LastSheet As Worksheet
Dim DestRange As Range
' Set the source folder
SourceFolder = "C:\My Documents"
' Use FileDialog to select the source file with filters
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select a Vat* File"
.InitialFileName = SourceFolder
.Filters.Clear
.Filters.Add "Excel Macro-Enabled Files", "*.xlsm"
.FilterIndex = 1
If .Show = -1 Then
SourceFile = .SelectedItems(1)
Else
Exit Sub
End If
End With
' Check if the file name contains "Vat" and ends with ".xlsm"
If InStr(1, SourceFile, "Vat") > 0 And Right(SourceFile, 5) = ".xlsm" Then
' Open the source workbook
Workbooks.Open SourceFile
Set LastSheet = ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)
' Copy D2 from the last sheet to "Imported Data"
LastSheet.Range("D2").Copy
ThisWorkbook.Sheets("Imported Data").Range("A1").PasteSpecial xlValues
ThisWorkbook.Sheets("Imported Data").Range("A1").PasteSpecial xlPasteFormats
' Copy AB2 to AD10 from the last sheet to "Imported Data"
LastSheet.Range("AB2:AD10").Copy
Set DestRange = ThisWorkbook.Sheets("Imported Data").Range("A2")
DestRange.PasteSpecial xlValues
DestRange.PasteSpecial xlPasteFormats
' Close the source workbook without saving
ActiveWorkbook.Close SaveChanges:=False
Else
MsgBox "Selected file does not match the criteria (Vat*.xlsm)."
End If
End Sub