[英]Count Rows in a Specific range
I am writing a command to copy and paste selections based on a cells values.我正在编写一个命令来复制和粘贴基于单元格值的选择。 Please see below for the example code.
请参阅下面的示例代码。
What i am struggling with is the entry destination then inputting data into the row below.我正在努力的是输入目的地,然后将数据输入到下面的行中。 The function works when used in bulk, but on repeated use, it will overwrite the data already in row 34 and then continue overwriting the rows below.
function批量使用时有效,但重复使用时会覆盖第34行已有的数据,然后继续覆盖下面的行。
What would be best for this purpose?什么最适合这个目的? a counta command or a row.count?
counta 命令还是 row.count? And if so, how would i write it?
如果是这样,我将如何写它?
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
To get the LastRow
dynamically, for Column A:要动态获取
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
If I understand correctly, you want the 115 value to be inserted to the next blank row on Column B, you could assign the pasteRowIndex
the same way like so:如果我理解正确,您希望将 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
If the pasteRowIndex
must be between rows rows 34 and 64 put in an If...Then...Else
statement.如果
pasteRowIndex
必须在第 34 行和第 64 行之间,则放入If...Then...Else
语句。 Something like:就像是:
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
Same principle with a static range:与 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.