我正在自動化我的團隊每個月完成的作業簿,并且遇到了用于回傳 Find Greater Than 的結果的代碼。
我最初并沒有撰寫這段代碼,我在 Stackoverflow 上找到了它并根據我的目的進行了調整。原始代碼來自: excel vba copy cells to another sheet if cell value is greater than 0
在我的作業表“代理計數”中,我在 A、B、C 列中有資訊,其中 C 包含數字計數結果。代碼找到任何大于 50 的計數。當代碼在 C 列中找到大于 50 的計數時,它會將三個單元格復制并粘貼到同一張作業表上的新位置,從“F2”開始。創建一個單獨的大于 50 的計數匯總表。
該代碼成功地找到并復制和粘貼大于 50 的計數。但是在粘貼結果后它不會向下移動到下一行。因此將下一個結果粘貼到上一個結果的頂部。
如何撰寫代碼以使每個結果的粘貼向下移動到 F2、F3、F4 等行?
Sub FindGreaterThan50V3()
Dim range1 As Range
Dim cell As Range
Set range1 = Sheets("Agent Count").Range("c:c")
For Each cell In range1
If cell.Value > 50 Then
With Sheets("Agent Count")
.Range(.Cells(cell.Row, "a"), .Cells(cell.Row, "c")).Copy _
Sheets("agent count").Range("f2").End(xlUp).Offset(1, 0)
End With
End If
Next cell
End Sub
uj5u.com熱心網友回復:
這:
.Range(.Cells(cell.Row, "a"), .Cells(cell.Row, "c")).Copy _
Sheets("agent count").Range("f2").End(xlUp).Offset(1, 0)
應該是:
.Range(.Cells(cell.Row, "a"), .Cells(cell.Row, "c")).Copy _
Sheets("agent count").Cells(Rows.Count, "F").End(xlUp).Offset(1, 0)
更多建議:
Sub FindGreaterThan50V3()
Dim range1 As Range, ws As WorkSheet
Dim cell As Range
Set ws = ThisWorkbook.Sheets("Agent Count")
'no need to scan the whole column
Set range1 = ws.Range("C1:C" & ws.cells(ws.Rows.Count, "C").End(xlUp).Row)
For Each cell In range1.Cells
If cell.Value > 50 Then
cell.Resize(1, 3).Copy _
ws.Cells(ws.Rows.Count, "F").End(xlUp).Offset(1, 0)
End If
Next cell
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/409006.html
標籤:
上一篇:在VBA中查找二維陣列中的重復項
