[英]Excel VBA Selecting all Rows with specific values in multiple columns
Hello I have a table with 1000 rows and 25 columns A through Y. I am trying to create a macro that will select ALL rows in the table which MUST contain a "0" in the "I" column AND a "2" in the "R" column.你好,我有一个包含 1000 行和 25 列 A 到 Y 的表。我正在尝试创建一个宏,它将 select 表中的所有行必须包含“I”列中的“0”和“I”列中的“2” “R”列。
I would then like to get the data from the NEXT ROW UP from the selected rows in the first part of the macro.然后我想从宏第一部分的选定行中获取 NEXT ROW UP 的数据。 IF the value in columns "I" is ABOVE 0, sum up how many times and if the value is BELOW 0, sum up how many times.
如果“I”列中的值高于 0,则求和次数,如果值低于 0,则求和次数。
This is what I have so far:这是我到目前为止所拥有的:
Sub newCode()
Dim i As Long: i = 1
nextValue As Integer
Dim rng As Range
Do While Range("I" & i).Value = 0 And Range("R" & i).Value = 2
i = i + 1
Loop
Range("I1:I" And "R1:R" & i - 1).EntireRow.Select
'getting mismatch error
Next
nextValue = rng.Offset(1, 0).Value
If nextValue > 0 Then
'Need help here
ElseIf nextValue < 0 Then
'Need help here
Else
End Sub
here is something that does what you describe:这是你描述的东西:
Sub newCode()
Dim nextValue As Integer
Dim row As Range
Dim sel As Range
Dim countPos As Integer
Dim countNeg As Integer
For i = 1 To 1000
If Range("I" & i).Value = 0 And Range("R" & i).Value = 2 Then
Set row = Range("A" & i).EntireRow
If sel Is Nothing Then
Set sel = row
Else
Set sel = Application.Union(sel, row)
End If
sel.Select
nextValue = Range("I" & i + 1).Value
If nextValue > 0 Then
countPos = countPos + 1
ElseIf nextValue < 0 Then
countNeg = countNeg + 1
End If
End If
Next
MsgBox "Count of positive values: " & countPos & vbCrLf & _
"Count of negative values: " & countNeg
End Sub
It processes exactly 1000 rows, selects the rows having a "0" in column I:I and a "2" in column R:R, and return (*) the count of rows that:它恰好处理 1000 行,选择在列 I:I 中具有“0”和在列 R:R 中具有“2”的行,并返回 (*) 行数:
(*) You might need to display the Execution Window in the Visual Basic Editor to see the result produced by the procedure. (*) 您可能需要在 Visual Basic 编辑器中显示执行 Window 以查看过程产生的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.