我刚接触VBA。
VBA选择3行,但仅计数1?我找不到类似的案例,我可以理解。
我无法粘贴整个代码,因为stackoverflow不允许进行此类发布。
下面的代码。
For i = 39 To lastRow
' Look for identical rows as input
If Cells(i, 3) = Range("C29") And Cells(i, 4) = Range("D29") And Cells(i, 5) = Range("E29") And Cells(i, 6) = Range("F29") And Cells(i, 7) = Range("G29") And Range("H29") = Cells(i, 8) Then
If foundRange Is Nothing Then
'Defines the first row as foundrange, if nothing found earlier
Set foundRange = Range(Cells(i, 3), Cells(i, 8))
Else
'Adds rows to foundRange, if one (or more) rows are found
Set foundRange = Union(foundRange, Cells(i, 3), Cells(i, 4), Cells(i, 5), Cells(i, 6), Cells(i, 7), Cells(i, 8))
End If
End If
Next i
If foundRange Is Nothing Then
MsgBox "No rows found acording to input"
Else
'Count amount of rows to be removed
foundRange.Rows.Select
MsgBox selection.Rows.Count
答案 0 :(得分:2)
如果您不需要选择单元格或将其存储在foundRange
中供以后使用,则只需使用CountIfs
,如下所示:
With ActiveSheet.Range(ActiveSheet.Cells(39, 3), ActiveSheet.Cells(lastRow, 8))
MsgBox WorksheetFunction.CountIfs(.Columns(1), Range("C29").Value, _
.Columns(2), Range("D29").Value, _
.Columns(3), Range("E29").Value, _
.Columns(4), Range("F29").Value, _
.Columns(5), Range("G29").Value, _
.Columns(6), Range("H29").Value)
End With
如果要做需要将单元格存储在某个范围内,那么您可能需要浏览Areas
中的Range
:
'Most of your code here...
If foundRange Is Nothing Then
MsgBox "No rows found acording to input"
Else
'Count amount of rows to be removed
foundRange.Rows.Select 'Is this line necessary?
Dim RangeArea As Range, TotalRows AS Long: TotalRows = 0
'N.B. If your Areas can be in different Columns of the same Row:
' use foundRange.EntireRow.Areas to prevent double-counting!
For Each RangeArea In foundRange.Areas
TotalRows = TotalRows + RangeArea.Rows.Count
Next RangeArea
MsgBox TotalRows
End If