[英]range.rows.count isn't updating on loop
Intended functionality is: for each item in list A, scan list B and identify matches. 预期的功能是:对于列表A中的每个项目,扫描列表B并识别匹配项。 Add these matches to a range, which is then output if the match count is > 0. 将这些匹配项添加到一个范围中,如果匹配计数> 0,则将其输出。
This works perfect the first time though the loop, but on all further iterations, while WorkRng is added to properly (I can output it to a cell and it has all the data), WorkRng.Rows.Count remains at 1. 第一次通过循环就可以完美运行,但是在所有后续迭代中,当正确添加WorkRng(我可以将其输出到单元格并包含所有数据)时,WorkRng.Rows.Count保持为1。
Dim pasteRng as Range
Dim WorkRng as Range
For i = 2 To LastRow
Set WorkRng = Sheets("Output").Range("A1:G1")
For j = 2 To outputCount
If Worksheets("Output").Cells(j, 8).Value = Worksheets("Vendor List").Cells(i, 1).Value Then 'this line correctly identifies matches on subsequent loops
Set pasteRng = Sheets("Output").Range("A" & j & ":G" & j)
Set WorkRng = Union(WorkRng, pasteRng) 'this line does not increase WorkRng.Rows.Count except on the first loop
End If
Next j
If WorkRng.Rows.Count > 1 Then
'do some stuff
End If
Set pasteRng = Nothing
Set WorkRng = Nothing
Next i
I would appreciate any guidance. 我将不胜感激。
If you skip rows you end up with a multi-area (non-contiguous) range, so by default Rows.Count will only return the number of rows in the first Area (ie. 1) 如果您跳过行,则最终将得到一个多区域(非连续)范围,因此默认情况下,Rows.Count将仅返回第一个Area(即1)中的行数。
In the Immediate pane: 在立即窗格中:
? Range("$A$1:$C$1,$A$3:$C$3").Rows.Count
>> 1
You could update your If check: 您可以更新您的If检查:
If WorkRng.Cells.Count > 6 Or Then
'do some stuff
End If
You can this generic function to count rows from any range (both contiguous and non-contiguous): 您可以使用此泛型函数来计算任何范围(连续和非连续)中的行:
Function GetRowsCount&(rng As Range)
Dim rngArea As Range
For Each rngArea In rng.Areas
GetRowsCount = GetRowsCount + rngArea.Rows.Count
Next
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.