I have two workbooks "Source" and "Target". I want to go through each sheet of Target workbook and find all the named ranges. If the range name is the same with the name of the sheet (from the other workbook: Source) then I will copy the data from Source to Target.
- Let's say in Source I have a sheet ("Test1") and in Target I have a named range ("Test1") in a random sheet. I want to match those and copy data from sheet "Test1" to Range "Test1".
But I can't figure out how to refer to a dynamic range/array.. I would like to use array because it is faster. Any ideas please? Thanks is advance. This is what I have tried so far:
Public Sub test()
Dim wb1 As Workbook, wb2 As Workbook
Set wb1 = Workbooks("Source.xlsx")
Set wb2 = Workbooks("Target.xlsx")
For Each ws2 In wb2.Worksheets
For Each nm In wb2.Names
For Each ws1 In wb1.Worksheets
If ws1.Name = nm.Name Then
'Here is the problem, I don't know how to define this dynamic array properly:
'It should be arrTrgt3 = ws2.Range("A1:D5") but I want to use named range, and I don't
'know this name yet.
arrTrgt3 = nm.RefersToRange.Address
ws2.Range("A1").Resize(UBound(arrTrgt3), UBound(arrTrgt3, 2)) = arrTrgt3
End If
Next
Next
Next
End Sub