我正在尝试从Sheet2中进行多个选择。该值来自同一列但不同的行(考虑是否使用ActiveCell.Offset(1,0)是可行的)。
我的代码从ActiveCell选择中获取值,然后运行一个宏,将其与另一个工作表(Sheet10)进行比较,并复制一些信息并将其粘贴到目标工作表(Sheet5)中。
以下是我现在拥有的代码。
a = Sheet10.Cells(Rows.Count, 1).End(xlUp).Row
c = Sheet2.Cells(Rows.Count, 5).End(xlUp).Row
For Each cell In Range(ActiveCell, ActiveCell.Offset(1, 0))
For i = 2 To a 'from Row 1 to the last row of "DMP"
Debug.Print ("i = " & i)
If cell.Value = Sheet10.Cells(i, 1).Value Then 'if selected cell matches (i,1) of "Sheet10 (DMP)"
Debug.Print ("ActiveCell =" & ActiveCell.Value)
For k = 1 To 20 'from Column 1 to Column 20
Debug.Print ("k = " & k)
For r = 1 To c 'from Row 1 to the last row of "Sheet 2(LightOn SKU)"
Debug.Print ("r = " & r)
If Sheet10.Cells(i, k).Value = Sheet2.Cells(r, 4).Value Then 'if value of (i,k) of "Sheet10 (DMP)" = (r,4) of "Sheet2 (LightOn SKU)"
Sheet2.Range("A" & r & ":G" & r).Copy
Sheet5.Activate
b = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row
Sheet5.Cells(b + 1, 1).Select
ActiveSheet.Paste
Range("A" & r & ":L" & r).Borders.Color = vbBlack
End If
Next
Next
End If
Next
Next
现在,它正在无休止的循环中运行。
答案 0 :(得分:1)
Sub ACCopy2()
Dim a As Long
Dim c As Long
Dim r As Long
Dim i As Long
Dim k As Integer
Dim b As Long
a = Sheet10.Cells(Rows.Count, 1).End(xlUp).Row c = Sheet2.Cells(Rows.Count, 5).End(xlUp).Row For r = 1 To c ' from Row 1 to the last row of "Sheet 2(LightOn SKU)" Debug.Print ("r = " & r) For i = 2 To a ' from Row 1 to the last row of "DMP" Debug.Print ("i = " & i) ' if selected cell matches (i,1) of "Sheet10 (DMP)" If Sheet2.Cells(r, 1).Value = Sheet10.Cells(i, 1).Value Then Debug.Print ("Sheet2 =" & Sheet2.Cells(r, 1).Value) For k = 1 To 20 ' from Column 1 to Column 20 Debug.Print ("k = " & k) ' if value of (i,k) of "Sheet10 (DMP)" = (r,4) of ' "Sheet2 (LightOn SKU)" If Sheet10.Cells(i, k).Value = Sheet2.Cells(r, 4).Value Then With Sheet5 b = Sheet5.Cells(Rows.Count, 1).End(xlUp).Row Sheet2.Range("A" & r & ":G" & r).Copy .Cells(b + 1, 1) .Range("A" & r & ":L" & r).Borders.Color = vbBlack End With End If Next End If Next Next
End Sub