【发布时间】:2016-12-07 16:21:01
【问题描述】:
我是 VBA 编码的新手,请帮助我创建具有以下条件的 VBA 脚本。
- 应突出显示包含小数的单元格。
- 应突出显示字符数少于 3 或多于 6 的单元格。
- 应该从 G 列 (G1) 执行到最后一行最后使用的单元格。
我的数据是字母数字或数字。
我尝试过使用characters.count 和Value.count,但没有成功。希望它适用于len,但我不知道如何开始。
附件是带有突出显示单元格的示例 excel 文件
我已经尝试了下面的代码。因为我的数据是字母数字的,所以字符数没有帮助。
Sub HighlightCells()
Range(" G1").Select
Do
If ActiveCell.Characters.Count < 3 Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row last used cell
Loop Until ActiveCell = ""
Range(" G1").Select
Do
If ActiveCell.Characters.Count > 6 Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
ActiveCell.Offset(0, 1).Select 'need to run in every row till the last row last used cell
Loop Until ActiveCell = ""
End Sub
【问题讨论】:
-
条件是AND条件还是OR条件???
-
如果单元格包含 no 个字符,是否要突出显示它???
-
条件只有 OR。没有字符的空白单元格不需要突出显示。