我在同一个 excel 文件中有两个数据表: Sheet1 作为“数据”,有 7 列:
第二张表是“主要”,有 5 列:
匹配两个文件的同一列是“名称”。我想要一个与两个工作表上的名称匹配的 VBA 代码,并通过匹配两个工作表上的列名来将数据从 proc1 - Proc4 从工作表“Main”复制到工作表“数据”。
我在堆栈溢出中搜索了类似的问题,这是我找到的代码(稍作修改):
Sub CopyData()
Dim shtImport As Worksheet
Dim shtMain As Worksheet
Set shtImport = ThisWorkbook.Sheets("Data")
Set shtMain = ThisWorkbook.Sheets("Main")
Dim CopyColumn As Long
Dim CopyRow As Long
Dim LastColumn As Long
'- for each column in row 1 of import sheet
For CopyColumn = 1 To shtImport.Cells(1, shtImport.Columns.Count).End(xlToRight).Column
'- check what the last column is with data in column
LastRowOfColumn = shtImport.Cells(shtImport.Columns.Count, CopyColumn).End(xlToRight).Column
'if last column was larger than one then we will loop through rows and copy
If LastColumn > 1 Then
For CopyRow = 1 To LastColumn
'- note we are copying to the corresponding cell address, this can be modified.
shtMain.Cells(CopyRow, CopyColumn).value = shtImport.Cells(CopyRow, CopyColumn).value
Next CopyRow
End If
Next CopyColumn
End Sub
这不是我想要的工作方式。有人可以帮我解决这个问题。非常感谢!