比较三列,使用不匹配的条件。

huangapple go评论69阅读模式
英文:

compare between 3 columns with unmatched criteria

问题

在代码中,您想要排除那些列B中包含"No"的行。以下是翻译好的代码部分:

' 在列B中,如果包含"No",则排除该行
For r = 1 To rCount
    uStr = CStr(uData(r, 1))
    If dict.Exists(uStr) Then
        If StrComp(CStr(vData(r, 1)), dict(uStr), vbTextCompare) <> 0 Then
            If urg Is Nothing Then
                Set urg = rg.Rows(r)
            Else
                Set urg = Union(urg, rg.Rows(r))
            End If
        End If
    Else
        dict(uStr) = CStr(vData(r, 1))
    End If
Next r

' 如果存在要排除的行,则设置其背景颜色
If Not urg Is Nothing Then
    rg.Interior.ColorIndex = xlNone
    urg.Interior.Color = X
End If

这部分代码将检查列B中的值,如果包含"No",则将相应的行添加到urg 变量中,并设置其背景颜色为X。

英文:

I have a set of 3 columns with data, I want to Exclude the rows that say "No" from the compression mode, if you could kindly let me know what I missing in this piece of code, I'd be much appreciated.

In column B where it says "No, " discard this row from the comparison.

Dim uData (), vData(), rg as range, rCount As Long, X as variant
Dim  urg As Range, r As Long, uStr As String, i As Integer
Dim  dict As Object
X = RGB(200, 205, 5)
With sheets(&quot;Data&quot;)
    With Intersect(.UsedRange, .Range(&quot;A15:C2000&quot;))
        rCount = .Rows.Count
        Set rg = .Resize(rCount)
    End With
End With

uData = rg.Columns(1).Value
vData = rg.Columns(3).Value
Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
dict.CompareMode = vbTextCompare
For r = 1 To rCount
    uStr = CStr(uData(r, 1))
    If dict.Exists(uStr) Then
        If StrComp(CStr(vData(r, 1)), dict(uStr), vbTextCompare) &lt;&gt; 0 Then                                                 
            If urg Is Nothing Then
                Set urg = rg.Rows(r)
            Else
                Set urg = Union(urg, rg.Rows(r))
            End If
        End If
    Else
        dict(uStr) = CStr(vData(r, 1))
    End If
Next r

If Not urg Is Nothing Then
    rg.Interior.ColorIndex = xlNone
    urg.Interior.Color = X
End If

答案1

得分: 2

Option Explicit

Sub demo()
    Dim ar(), rg As Range, urg As Range
    Dim r As Long, colA As String, colB As String, colC As String
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare
    
    With Sheets("Data")
        Set rg = Intersect(.UsedRange, .Range("A15:C2000"))
        ar = rg.Value
    End With
   
    For r = 1 To UBound(ar)
        colB = Trim(ar(r, 2))
        If UCase(colB) <> "NO" Then
            colA = Trim(ar(r, 1))
            colC = Trim(ar(r, 3))
            If dict.Exists(colA) Then
                If StrComp(colC, dict(colA), vbTextCompare) <> 0 Then
                    If urg Is Nothing Then
                        Set urg = rg.Rows(r)
                    Else
                        Set urg = Union(urg, rg.Rows(r))
                    End If
                End If
            Else
                dict.Add colA, colC
            End If
        End If
    Next r
    
    If Not urg Is Nothing Then
        rg.Interior.ColorIndex = xlNone
        urg.Interior.Color = RGB(200, 205, 5)
    End If
End Sub
英文:
Option Explicit

Sub demo()
    Dim ar(), rg As Range, urg As Range
    Dim r As Long, colA As String, colB As String, colC As String
    Dim dict As Object
    Set dict = CreateObject(&quot;Scripting.Dictionary&quot;)
    dict.CompareMode = vbTextCompare
    
    With Sheets(&quot;Data&quot;)
        Set rg = Intersect(.UsedRange, .Range(&quot;A15:C2000&quot;))
        ar = rg.Value
    End With
   
    For r = 1 To UBound(ar)
        colB = Trim(ar(r, 2))
        If UCase(colB) &lt;&gt; &quot;NO&quot; Then
            colA = Trim(ar(r, 1))
            colC = Trim(ar(r, 3))
            If dict.Exists(colA) Then
                If StrComp(colC, dict(colA), vbTextCompare) &lt;&gt; 0 Then
                    If urg Is Nothing Then
                        Set urg = rg.Rows(r)
                    Else
                        Set urg = Union(urg, rg.Rows(r))
                    End If
                End If
            Else
                dict.Add colA, colC
            End If
        End If
    Next r
    
    If Not urg Is Nothing Then
        rg.Interior.ColorIndex = xlNone
        urg.Interior.Color = RGB(200, 205, 5)
    End If
End Sub

</details>



huangapple
  • 本文由 发表于 2023年6月11日 22:46:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定