|
本帖最后由 cmo9020 于 2023-2-19 22:03 编辑
B.rar
(27.81 KB, 下载次数: 9)
各位周未愉快~请问导师们一下....
Sheet6工作表要依据第1行标题和数据.xlsx里面的"数据表"工作表 B3行列的标题做对应,字符串要一模一样在把数据复制过去,需要怎么写
目前有点错乱,请导师们有空帮助一下,谢谢各位~谢谢
Sub CopyDataToAnotherWorkbook()
Dim destWorkbook As Workbook
Dim destWorksheet As Worksheet
Dim srcWorksheet As Worksheet
Dim destRow As Long
Dim lastRow As Long
Dim lastCol As Long
Dim i As Long, j As Long
Dim header As String
Set destWorkbook = Workbooks.Open(ThisWorkbook.path & "\数据.xlsx")
Set destWorksheet = destWorkbook.Sheets("数据表")
lastRow = destWorksheet.Cells(Rows.Count, 2).End(xlUp).Row
lastCol = destWorksheet.Cells(3, Columns.Count).End(xlToLeft).Column
header = destWorksheet.Cells(3, 2).Value
Set srcWorksheet = ThisWorkbook.Sheets("Sheet6")
For i = 2 To lastCol
If destWorksheet.Cells(3, i).Value = header Then
For j = 2 To srcWorksheet.Cells(Rows.Count, 1).End(xlUp).Row
If srcWorksheet.Cells(j, i).Value <> "" Then
destRow = lastRow + j - 1
destWorksheet.Cells(destRow, 2).Resize(1, lastCol - 1).Value = _
srcWorksheet.Cells(j, 2).Resize(1, lastCol - 1).Value
End If
Next j
Exit For
End If
Next i
destWorkbook.Close SaveChanges:=True
End Sub
|
|