我对VBA(或与此有关的任何代码)相对较新,并且使用不多,但是我一直在努力寻找方法。
我有两个用数据填充的Excel工作表。我正在尝试获取工作表1的A和B列中的零件号和下一个更高的零件号,并在工作表2的A和F列中寻找相同的零件号和下一个较高的零件号。我希望它将数据从工作表2拖到工作表1(例如,将工作表2的B列中的数据拖到工作表1的M列中。)
Sub EEE_Reformat()
Dim wb1 As Workbook
Dim cell1 As Range, rng1 As Range, cell2 As Range, rng2 As Range
Dim Cel As Range
Dim Sht1 As Worksheet
Dim Sht2 As Worksheet
Set wb1 = ThisWorkbook
Set Sht1 = wb1.Sheets("Sheet1")
Set Sht2 = wb1.Sheets("Sheet2")
Set Cel = Sht1.Range("A2")
Set rng1 = Range(Cel, Cel.Offset(Sht1.Cells.Rows.Count - Cel.Row, 0).End(xlUp))
Set Cel = Sht2.Range("A2")
Set rng2 = Range(Cel, Cel.Offset(Sht2.Cells.Rows.Count - Cel.Row, 0).End(xlUp))
For i = 2 To 2500
If Sht1.Cells(i, 13) = "" Then 'if current cell in column 13 is empty then...
For Each cell2 In rng2 'for each cell in range 2 defined above (column A in Sheet2)...
For Each cell1 In rng1 'for each cell in range 1 defined above (column A in Sheet1)...
If cell2.Value = cell1.Value And cell2.Offset(i, 5) = cell1.Offset(i, 1).Value Then 'if the value of cell2 equals the value of cell1 AND the value of cell2 (offset by 5 columns) equals the value of cell1 (offset by 1 columns) then...
cell1.Offset(i, 12).Value = cell2.Offset(i, 1).Value 'from to Sheet1 column B to Sheet2 column M
cell1.Offset(i, 13).Value = cell2.Offset(i, 2).Value 'from to Sheet1 column C to Sheet2 column N
Exit For
End If
Next
Next
End If
Next
End Sub