我尝试连接两个单元格并根据" U"的存在来格式化子字符串。在字符串中。我已经查看了其他一些线程来构建下面的代码,但是它挂起了.characters命令行。
串联也是基于B,C和D列中的值比较相同的迭代。在连续连接期间,红色字符是否会保留在原位,或者我最好根据结果字符串中包含U的部分来设置最终字符串的格式?
例如,如果H列值按顺序在下面的循环中连接在一起,如下所示:
我非常感谢任何帮助。 TIA。
Sub CustomFormat()
' CustomFormat Macro
Dim LR As Long, Rw As Long, FinishPoint As Integer
LR = Range("A" & Rows.Count).End(xlUp).Row
For Rw = LR To 1 Step -1
If Range("B" & Rw).Value = Range("B" & Rw - 1).Value And _
Range("C" & Rw).Value = Range("C" & Rw - 1).Value And _
Range("D" & Rw).Value = Range("D" & Rw - 1).Value Then
Range("H" & Rw - 1).Value = Range("G" & Rw - 1).Value & vbCrLf & Range("G" & Rw).Value
End If
If InStr(1, Range("G" & Rw - 1).Value, "U") Then
FinishPoint = Len(Range("G" & Rw - 1).Value)
With Range("H" & Rw - 1).Value
With .Characters(1, FinishPoint).Font.Color = vbRed
End With
End With
End If
Next Rw
答案 0 :(得分:1)
这个应该有效:
Sub CustomFormat()
Dim LR As Long, Rw As Long, i As Byte
Dim FinishPoint As Integer, StartPoint As Integer
LR = Range("A" & Rows.Count).End(xlUp).Row
For Rw = LR To 2 Step -1
If Range("B" & Rw).Value = Range("B" & Rw - 1).Value And _
Range("C" & Rw).Value = Range("C" & Rw - 1).Value And _
Range("D" & Rw).Value = Range("D" & Rw - 1).Value Then
Range("H" & Rw - 1).Value = Range("G" & Rw - 1).Value & vbCrLf & Range("G" & Rw).Value
If InStr(1, Range("G" & Rw - 1).Value, "U") Then
FinishPoint = Len(Range("G" & Rw - 1).Value)
With Range("H" & Rw - 1)
.Characters(1, FinishPoint).Font.Color = vbRed
End With
End If
If InStr(1, Range("G" & Rw).Value, "U") Then
StartPoint = Len(Range("G" & Rw - 1).Value) + 2
FinishPoint = Len(Range("G" & Rw).Value) + StartPoint
With Range("H" & Rw - 1)
.Characters(StartPoint, FinishPoint).Font.Color = vbRed
End With
End If
End If
Next Rw
End Sub
请注意,InStr(1, Range("G" & Rw - 1).Value, "U")
区分大小写。如果单元格包含u
,则返回0
。您可以将其更改为:InStr(1, UCase(Range("G" & Rw - 1).Value), "U")