I have tried to write code to copy the entire range from sheet "Journals" in the source file and paste as values in destination sheet , where the names are similar for eg source file name is BR1 Southern (BTR) Tax Journals.xlsx . The Destination sheet for eg is "BR1 Southern" so there is a partial match between the destination sheet and source file , so the data must be pasted into this sheet
I have several source file where the names are similar to the destination sheet and they must copied using the same criteria
I cannot get the data to paste on the appropriate sheets
The folder is C:\Tax Journals & Computations Year End
Kindly amend my Code
I have several source file where the names are similar to the destination sheet and they must copied using the same criteria
I cannot get the data to paste on the appropriate sheets
The folder is C:\Tax Journals & Computations Year End
Kindly amend my Code
Code:
Sub CopyJournalDataToMatchingSheets()
Dim fd As FileDialog
Dim FilePath As String
Dim SourceWorkbook As Workbook
Dim DestinationWorkbook As Workbook
Dim SourceSheet As Worksheet
Dim DestinationSheet As Worksheet
Dim SourceFileName As String
Dim CleanFileName As String
Dim DestinationSheetName As String
Dim LastRow As Long
Dim i As Integer
Dim SourceFileNamePart As String
' Set the destination workbook
Set DestinationWorkbook = ThisWorkbook
' Initialize FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Select Files from Tax Journals Folder"
.InitialFileName = "C:\Tax Journals & Computations Year End\"
.Filters.Clear
.Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm"
.AllowMultiSelect = True
' Show dialog and exit if canceled
If .Show = False Then
MsgBox "No files selected. Exiting macro.", vbExclamation
Exit Sub
End If
End With
' Loop through each selected file
For i = 1 To fd.SelectedItems.Count
FilePath = fd.SelectedItems(i)
' Open the source workbook
On Error Resume Next
Set SourceWorkbook = Workbooks.Open(FilePath, ReadOnly:=False)
If Err.Number <> 0 Then
MsgBox "Could not open file: " & FilePath, vbExclamation
Err.Clear
On Error GoTo 0
GoTo NextFile
End If
On Error GoTo 0
' Extract the source file name (without extension)
SourceFileName = Replace(Dir(FilePath), ".xlsx", "")
SourceFileName = Replace(SourceFileName, ".xlsm", "")
SourceFileName = Replace(SourceFileName, ".xls", "")
SourceFileName = Trim(SourceFileName)
' Remove any text within parentheses and the parentheses themselves
If InStr(SourceFileName, "(") > 0 Then
CleanFileName = Trim(Replace(SourceFileName, Mid(SourceFileName, InStr(SourceFileName, "("), InStr(SourceFileName, ")") - InStr(SourceFileName, "(") + 1), ""))
Else
CleanFileName = SourceFileName
End If
' Use the clean part of the file name as the matching string
SourceFileNamePart = CleanFileName
' Check if the source sheet "Journals" exists
On Error Resume Next
Set SourceSheet = SourceWorkbook.Sheets("Journals")
On Error GoTo 0
If Not SourceSheet Is Nothing Then
' Look for a destination sheet that partially matches the source file name part
For Each DestinationSheet In DestinationWorkbook.Sheets
DestinationSheetName = DestinationSheet.Name
' Check if the destination sheet name contains the source filename part
If InStr(1, DestinationSheetName, SourceFileNamePart, vbTextCompare) > 0 Then
' Copy data from the source sheet "Journals"
With SourceSheet
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
If LastRow > 0 Then
.Range("A1:A" & LastRow).EntireRow.Copy
End If
End With
' Paste data into the destination sheet starting at A1
With DestinationSheet
.Cells.Clear ' Optional: Clear existing data before pasting
.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
.Range("A1").PasteSpecial Paste:=xlPasteFormats
End With
Exit For ' Exit the loop once the matching sheet is found
End If
Next DestinationSheet
Else
MsgBox "Source sheet 'Journals' not found in: " & FilePath, vbExclamation
End If
' Close the source workbook without saving
SourceWorkbook.Close SaveChanges:=False
NextFile:
Next i
' Clean up
Application.CutCopyMode = False
MsgBox "Data copied successfully.", vbInformation
End Sub [code]