英文:
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("Data")
With Intersect(.UsedRange, .Range("A15:C2000"))
rCount = .Rows.Count
Set rg = .Resize(rCount)
End With
End With
uData = rg.Columns(1).Value
vData = rg.Columns(3).Value
Set dict = CreateObject("Scripting.Dictionary")
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) <> 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("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
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论