我正在比较两列A和B. A列和B列包含数据库中的ID。
ID长度为13位,但大多数情况下长度为11位。
我想要一个符合此标准的代码。如果匹配,则突出显示为绿色,否则显示为红色。
我已经尝试过条件格式化了。我很高兴,如果我能在代码中拥有它。
Sub RangeTest()
Dim targetWorksheet As Worksheet
Dim i As Long
Dim totalrows As Integer
For i = 2 To 112
Set targetWorksheet = Worksheets("Preparation sheet")
With targetWorksheet
Cells(i, 3) = IIf(Cells(i, 1) = Cells(i, 2), "Yes", "NO")
Cells(i, 3).Interior.Color = IIf(Cells(i, 3) = "Yes", RGB(0, 255, 0), RGB(255, 0, 0))
End With
Next
End Sub
答案 0 :(得分:0)
尝试以下代码:
Option Explicit
Sub RangeTest()
Dim targetWorksheet As Worksheet
Dim i As Long
Dim totalrows As Integer
For i = 2 To 112
Set targetWorksheet = Worksheets("Preparation sheet")
With targetWorksheet
If Left(.Cells(i, 1), 11) = Left(.Cells(i, 2), 11) Then
.Cells(i, 3) = "Yes"
.Cells(i, 3).Interior.Color = RGB(0, 255, 0)
Else
.Cells(i, 3) = "NO"
.Cells(i, 3).Interior.Color = RGB(255, 0, 0)
End If
End With
Next i
End Sub