I have a Master File and Source File.
Master File: multiple worksheets, Column A to J
Source File: multiple worksheets, Column A to E
I have managed to come out with
1) Prompt window to select Source File
2) Copy Column C, D, E from Source File and Paste it to Column C, D, E by matching value in Column A within a worksheet
I am now trying to repeat this copy-paste in multiple worksheets.
The worksheet name ranges from "F.01, F.02 to F.10", "T.01, T.02 to T.10", "IS.01 to IS.05" for both Master and Source File.
I am a newbie in VBA - I managed to get below code done by checking all forums and forums.
Really need your help in helping me to code something along the line making it Copy-Paste to be repetitive based on range of worksheets as stated above.
Source "F.01" to Master "F.01" and it goes on
Sub CommandButton2_Click()
Dim fileDialog As fileDialog
Dim strPathFile As String
Dim strFileName As String
Dim strPath As String
Dim dialogTitle As String
Dim wbSource As Workbook
Dim rngToCopy As Range
Dim rngRow As Range
Dim rngDestin As Range
Dim lngRowsCopied As Long
Dim dic As Object
Dim ky As Variant
Dim c As Variant
Dim cel As Range
Dim x As String
Dim tgt As Range
Dim FR As Long
Dim SSheetList As Variant
Dim MSheetList As Variant
dialogTitle = "Navigate to and select required file."
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
.InitialFileName = "C:\Users\User\Documents"
'.InitialFileName = ThisWorkbook.Path & "\" 'Alternative to previous line
.AllowMultiSelect = False
.Filters.Clear
.Title = dialogTitle
If .Show = False Then
MsgBox "File not selected to import. Process Terminated"
Exit Sub
End If
strPathFile = .SelectedItems(1)
End With
Set wbSource = Workbooks.Open(Filename:=strPathFile)
SSheetList = Array("F.01", "F.02")
MSheetList = Array("F.01", "F.02")
Set sh1 = ThisWorkbook.Sheets(MSheetList)
Set sh2 = wbSource.Sheets(SSheetList)
Application.ScreenUpdating = False
If sh1 Is sh2 Then
For Each c In sh2.Range("A2", sh2.Range("A" & Rows.Count).End(xlUp))
FR = 0
On Error Resume Next
FR = Application.Match(c, sh1.Columns(1), 0)
On Error GoTo 0
If FR <> 0 Then sh1.Range("C" & FR).Value = c.Offset(, 2)
If FR <> 0 Then sh1.Range("D" & FR).Value = c.Offset(, 3)
If FR <> 0 Then sh1.Range("E" & FR).Value = c.Offset(, 4)
Next c
End If
wbSource.Close SaveChanges:=False
Set fileDialog = Nothing
Set rngRow = Nothing
Set rngToCopy = Nothing
Set wbSource = Nothing
Set rngDestin = Nothing
'MsgBox "The data is copied"
End Sub