我有两个相同的工作簿,其中有相同的命名范围。在其中一个工作簿(wbSource)中,这些命名范围包含数据,而在另一工作簿(wbTarget)中,这些命名范围为空。我正在尝试提出一种解决方案,以解决如何将指定范围内的数据从wbSource传输到wbTarget。如果有人在这里可以帮助我,我将不胜感激。我尝试使用下面的代码,但没有成功。
Sub dataTransfer()
Dim targetModel, sourceModel, fileC As String
Dim rangesArray As Variant
targetModel = "Target.xlsm"
sourceModel = "Source.xlsm"
fileC = "C:\mypath\"
Dim wbSource As Workbook: Set wbSource = Workbooks.Open(Filename:=fileC & sourceModel)
Dim wbTarget As Workbook: Set wbTarget = Workbooks.Open(Filename:=fileC & targetModel)
rangesArray = Array("Coconut", "Apple", "Pear") 'An array of the names for the named ranges in both wbSource and wbTarget.
For Each element in rangesArray
wbTarget.Range(element) = wbSource.Range(element)
Next element
End Sub