繁体   English   中英

Excel VBA 选择多列中具有特定值的所有行

[英]Excel VBA Selecting all Rows with specific values in multiple columns

你好,我有一个包含 1000 行和 25 列 A 到 Y 的表。我正在尝试创建一个宏,它将 select 表中的所有行必须包含“I”列中的“0”和“I”列中的“2” “R”列。

然后我想从宏第一部分的选定行中获取 NEXT ROW UP 的数据。 如果“I”列中的值高于 0,则求和次数,如果值低于 0,则求和次数。

这是我到目前为止所拥有的:

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

这是你描述的东西:

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

它恰好处理 1000 行,选择在列 I:I 中具有“0”和在列 R:R 中具有“2”的行,并返回 (*) 行数:

  • 就在这些选定行的下方(I 列中的“2”:I 和 R 列中的“0”:R)
  • 在 I:I 列中具有正值或负值。

(*) 您可能需要在 Visual Basic 编辑器中显示执行 Window 以查看过程产生的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM