如何在循环VBA中为不同范围编写两个IF语句

时间:2019-01-16 10:52:54

标签: excel vba loops if-statement range

我正在使用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,我不希望有任何空白单元格或零。

4 个答案:

答案 0 :(得分: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?

答案 1 :(得分:1)

您想要这样的东西吗?

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

答案 2 :(得分:1)

两个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

答案 3 :(得分:0)

我通常使用数组:

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

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