我有一个excel表,其中包含列中的excel工作簿列表以及另一列中的相应数据。我希望能够打开每个工作簿,并仅使用相应的数据填充特定列。目前,我的代码打开每个工作簿并附加其下的其他工作簿的数据。
我的代码:
Sub Workbook_Open()
Dim r1 As Range, r2 As Range, N As Long, r As Range, x As Range
Workbooks.Open "C:\Users\HP\Documents\test\script.xlsx"
For Each r In Range("A1:A3")
With r
Workbooks.Open r
Set Rng = Sheets("Sheet1").Range("B1:B3")
For Each cell In Rng
With cell
Set r2 = Sheets("Sheet1").Range("Y3:Y3")
Rng.Copy r2
End With
Next cell
End With
Next r
End Sub
您的意见将很高兴。
我真的不明白你在复制什么,但我假设你想打开主文件A列中的每个文件,从该文件中提取一些单元格并将它放在A列右边的单元格中。这样可以转移一对细胞,所以希望你可以找出如何修改你的需求,或者如果没有,你可以回来。
Sub Workbook_Open()
Dim r1 As Range, rng As Range, N As Long, r As Range, x As Range, wbMaster As Workbook, wb As Workbook
Set wbMaster = Workbooks.Open("C:\Users\HP\Documents\test\script.xlsx")
For Each r In wbMaster.Sheets(1).Range("A1:A3")
Set wb = Workbooks.Open(r)
wb.Sheets("Sheet1").Range("B1").value=r.Offset(, 1).Value 'this fills B1 of the workbook opened with B of the master file
wb.close false
Next r
End Sub