我一直试图做这件事但我无法做到。我通常会搜索更多等等,但我不熟悉VBA(只有C ++和Java)。
我有一个包含多个工作表的Excel文件(工作簿),这些工作表都包含数据(源)。我有另一个Excel文件,它再次包含多个工作表,只包含标题(目标)。
我想要做的是一个代码,它转到目标工作簿并获取标题,然后转到源工作簿并循环遍历所有工作表,直到找到此标题,然后将数据复制到目标工作簿中。 / p>
请记住,源文件中的所有标题都位于所有工作表的同一行。
答案 0 :(得分:1)
此代码可以解决您的问题;
Sub Read_External_Workbook()
'''''Define Object for Target Workbook
Dim Target_Workbook As Workbook
Dim Source_Workbook As Workbook
Dim Target_Path As String
'''''Assign the Workbook File Name along with its Path
'''''Change path of the Target File name
Target_Path = "D:\Sample.xlsx"
Set Target_Workbook = Workbooks.Open(Target_Path)
Set Source_Workbook = ThisWorkbook
'''''With Target_Workbook object now, it is possible to pull any data from it
'''''Read Data from Target File - Assuming that your headers's locations are absolute.
Target_Data = Target_Workbook.Sheets(1).Cells(1, 1)
Source_Workbook.Sheets(1).Cells(1, 1) = Target_Data
'''''Update Target File -
Source_data = Source_Workbook.Sheets(1).Cells(3, 1)
Target_Workbook.Sheets(1).Cells(2, 1) = Source_data
'''''Close Target Workbook
Source_Workbook.Save
Target_Workbook.Save
Target_Workbook.Close False
'''''Process Completed
MsgBox "Task Completed"
End Sub