【问题标题】:How to write two IF statements for different ranges in a loop, VBA如何在循环中为不同范围编写两个 IF 语句,VBA
【发布时间】:2019-06-10 10:41:45
【问题描述】:

我正在使用 VBA 处理 Excel 文档。本文档包含一个包含多列的数据库,但为简单起见,假设我有 2 列:

  • C 列对应名称
  • F 列对应数字。

我正在尝试创建一个宏来检查 F 列中的所有数字(带有循环)。如果数字大于 100,则检查 C 列中的相邻单元格。如果名称对应于条件(假设对应于 John 或 Tom),则在另一张表中添加数字的值。如果这些都不适用,请检查下一个单元格。

我的问题是我找不到在 C 列中定义单元格的方法(创建变量/对象来调用单元格或直接调用相邻的单元格)。

我的代码如下所示:

Sub Test1()

    Dim rngnumbers, rngnames, MultipleRange As Range

    Set rngnumbers = Sheet2.Range("F2:F999")    
    Set rngnames = Sheet2.Range("C2:C999")
    Set MultipleRange = Union(rngnumbers, rngnames)

        For Each numb In rngnumbers
            If numb.Value >= 100 Then
                    If Sheet2.Range("C2") = "John" Or Sheet2.Range("C2") = "Tom" Then '''The problem here is that it only looks at the cell C2 and not the adjacent cell
                        Sheet1.Range("I999").End(xlUp).Offset(1, 0).Value = numb.Value
                    Else
                    End If
            End If
        Next numb

End Sub

我尝试修改该行:

'If Sheet2.Range("C2") = "John" Or Sheet2.Range("C2") = "Tom" 然后' 类似于: 'newname.String = "John" '

但我找不到定义newname 的方法。 另一个想法是增加 For 循环中名称的 If 语句。

补充说明: 我也没有直接在 Excel 中使用公式,因为当 if 函数为 False 时,我不想要任何空白单元格或零。

【问题讨论】:

    标签: excel vba loops if-statement range


    【解决方案1】:

    这是否解决了您的问题 - 引用 C 列中的相关单元格? OFFSET 提供相对参考,在这种情况下,请查看 F 左侧的 3 列。

    Sub Test1()
    
    Dim rngnumbers As Range, rngnames As Range, MultipleRange As Range, numb As Range
    
    Set rngnumbers = Sheet2.Range("F2:F999")
    Set rngnames = Sheet2.Range("C2:C999")
    Set MultipleRange = Union(rngnumbers, rngnames)
    
    For Each numb In rngnumbers
        If numb.Value >= 100 Then
            If numb.Offset(, -3) = "John" Or numb.Offset(, -3) = "Tom" Then
                Sheet1.Range("I999").End(xlUp).Offset(1, 0).Value = numb.Value
            End If
        End If
    Next numb
    
    End Sub
    

    您是否考虑过使用 SUMIFS?

    【讨论】:

    • 您好 SJR,谢谢!使用偏移量解决了它。不,使用 SUMIFS 不是我目前正在寻找的。​​span>
    【解决方案2】:

    你想要这样的东西吗?

    Sub Test1()
    
            Dim lRow As Long, r As Long
            lRow = 1000 'last row in your data
            Dim ws As Worksheet
            Set ws = Worksheets("List with your data")
    
            For i = 2 To lRow
                If ws.Range("F" & i) > 100 Then
                    If ws.Range("C" & i).Value = "John" Or ws.Range("C" & i).Value = "Tom" Then
                        Worksheets("Another sheet sheet").Range("A" & r) = Range("C" & i).Value ' r - Row, where we want to enter uor text
                        r = r + 1 'if you want to put next name on the next row
                    End If
                End If
            Next
    
        End Sub
    

    【讨论】:

      【解决方案3】:

      循环中的两个 Ifs

      联合版

      Option Explicit
      
      Sub Test1()
      
          Const cFirst As Integer = 2
          Const cLast As Integer = 999
          Const cCol1 As Variant = "F"
          Const cCol2 As Variant = "C"
          Const cCol3 As Variant = "I"
      
          Dim i As Integer
          Dim rngU As Range
      
          With Sheet2
              For i = cFirst To cLast
                  If IsNumeric(.Cells(i, cCol1)) And .Cells(i, cCol1) >= 100 Then
                      If .Cells(i, cCol2) = "John" _
                              Or .Cells(i, cCol2) = "Tom" Then
                          If Not rngU Is Nothing Then
                              Set rngU = Union(rngU, .Cells(i, cCol1))
                            Else
                              Set rngU = .Cells(i, cCol1)
                          End If
                      End If
                  End If
              Next
          End With
      
          If Not rngU Is Nothing Then
              rngU.Copy Sheet1.Cells(cLast, cCol3).End(xlUp).Offset(1, 0)
              Set rngU = Nothing
          End If
      
      End Sub
      

      【讨论】:

        【解决方案4】:

        我通常使用数组:

        Sub Test1()
        
        Dim rngnumbers    As Excel.Range
        
        Dim arrVals       As variant 
        
        Dim lngRow        As long
        
        Arrvals = Sheet2.Range("C2:F999").value
        
            For Lngrow = lbound(arrvals,1) to ubound(arrvals,1)
                If arrvals(lngrow,4) >= 100 Then
                        If arrvals(lngrow,1)= "John" Or arrvals(lngrow,1) = "Tom" Then '''The problem here is that it only looks at the cell C2 and not the adjacent cell
                            Sheet1.Range("I999").End(xlUp).Offset(1, 0).Value = arrvals(lngrow,4)
                        Else
                        End If
                End If
            Next lngrow 
        
        End Sub
        

        其实我可能也会建一个输出数组,但是我的拇指很累......

        【讨论】:

          猜你喜欢
          • 2021-06-10
          • 1970-01-01
          • 1970-01-01
          • 2018-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-29
          • 1970-01-01
          相关资源
          最近更新 更多