简体   繁体   English

循环以在IF-THEN语句中进行单元比较

[英]Loop for cell comparison in IF-THEN statement

I need to compare each cell from range src.Range("A1:A" & 10) with cells in range src3.Range("A1:A" & 3) and proceed only if src cell is not equal to any of src3 cells. 我需要从范围内的每个细胞比较src.Range("A1:A" & 10)与单元格范围src3.Range("A1:A" & 3)只有继续进行,如果src细胞不等于任何的src3细胞。 It works if I manually list each comparison (as comment - what I have to get eventually. It works but there will be much greater range for comparison). 如果我手动列出每个比较,它就可以工作(作为注释-我最终要得到什么。它可以工作,但是比较的范围会更大)。 But it does not when I try to loop it. 但是当我尝试循环它时却没有。 I cannot separate IF from THEN . 我无法将IFTHEN分开。 And I cannot find alternative. 我找不到其他选择。

  For temprow = 1 To rngSelectionTable.Rows.Count                         
    tempselected = rngSelectionTable(temprow, 2).Value                      
    Crit = rngSelectionTable(temprow, 5).Value                              

    If tempselected = True Then

    For Each r In src.Range("A1:A" & 10)                

         'If r <> 0 _
          And r <> src3.Range("A1") _
          And r <> src3.Range("A2") _
          And r <> src3.Range("A3") _
          Then myFinalResult = r

        For Each comparisonRange In src3.Range("A1:A" & 3) 'does not work
          If r <> 0 And r <> comparisonRange 'does not work
              Next comparisonRange 'does not work
          Then myFinalResult = r 'does not work

        'rest of the code below
             If myFinalResult = Crit Then                                 
                 If CopyRange Is Nothing Then                        
                     Set CopyRange = r.EntireRow                 
                           Else
                     Set CopyRange = Union(CopyRange, r.EntireRow) 
                 End If
             End If   
       Next r
    End If
  Next temprow

Something like this should work: 这样的事情应该起作用:

Sub test()
    Dim r As Range, rng10 As Range, rng3 As Range
    Set rng10 = Sheet1.Range("a1:a10")
    Set rng3 = Sheet2.Range("$a$1:$a$3")

    For Each r In rng10
        If Application.WorksheetFunction.CountIf(rng3, r) > 0 Then
            'proceed here
        End If
    Next r
End Sub

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

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