[英]Count Rows in a Specific range
我正在編寫一個命令來復制和粘貼基於單元格值的選擇。 請參閱下面的示例代碼。
我正在努力的是輸入目的地,然后將數據輸入到下面的行中。 function批量使用時有效,但重復使用時會覆蓋第34行已有的數據,然后繼續覆蓋下面的行。
什么最適合這個目的? counta 命令還是 row.count? 如果是這樣,我將如何寫它?
Sub x115()
Dim r As Long, endRow As Long, pasteRowIndex As Long, y As Range
endRow = 31 ' last row
pasteRowIndex = 34 ' Entry Row but the range needs to be between B34 and B64
Set y = ActiveSheet.Range("B:M")
For r = 1 To endRow 'Loop through activesheet and search for 115
If Cells(r, "B").Value = 115 Then 'Found
'Copy the current row
Intersect(y, Rows(r)).Copy Cells(pasteRowIndex, 2)
Intersect(y, Rows(r)).ClearContents
pasteRowIndex = pasteRowIndex + 1
End If
Next r
End Sub
要動態獲取LastRow
,對於 A 列:
Dim LastRow As Long
LastRow = Sheets.Cells(Rows.Count, 2).End(xlUp).Row.
' the 1 in Cells(Row.Count, 1) indicates column 1 which is A
如果我理解正確,您希望將 115 值插入到 B 列的下一個空白行,您可以像這樣分配pasteRowIndex
:
Dim pasteRowIndex As Long
pasteRowIndex = Sheets.Cells(Rows.Count, 2).End(xlUp).Row.
' the 2 in Cells(Row.Count, 1) indicates column 1 which is B
如果pasteRowIndex
必須在第 34 行和第 64 行之間,則放入If...Then...Else
語句。 就像是:
If pasteRowIndex < 34 Or pasteRowIndex > 64 Then
MsgBox "PasteRowIndex =" & pasteRowIndex & vbNewLine & "It must be between rows 34 and 64."
Exit Sub
Else
'Continue with procedure.
End If
與 static 系列相同的原理:
Dim pasteRowIndex As Long
pasteRowIndex = Sheets.Cells(64, 2).End(xlUp).Row.
'You could change Cells(64, 2) with Range("B64") too.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.