Sub AddDuplicateFormatting 定义变量 Dim selectedRange As Range Dim rule As FormatCondition 获取选定区域 Set selectedRange = Selection 创建新的格式化规则 Set rule = selectedRangeFormatConditi
以下是已优化的代码:
Sub AddDuplicateFormatting()
'定义变量
Dim selectedRange As Range
Dim rule As FormatCondition
'获取选定区域
Set selectedRange = Selection
'检查是否选定了单元格
If selectedRange.Count < 2 Then
MsgBox "请选定至少两个单元格。"
Exit Sub
End If
'创建新的格式化规则
Set rule = selectedRange.FormatConditions.Add(Type:=xlDuplicateValues)
'设置格式化规则的样式
With rule
.Interior.ColorIndex = 6 '黄色
.Font.ColorIndex = 1 '黑色
.Font.Bold = True '加粗
End With
End Sub
优化的内容包括:
- 添加了检查是否选定了单元格的代码,避免出现无效的操作。
- 为了更好的代码可读性,将注释翻译成了中文。
原文地址: https://www.cveoy.top/t/topic/bUr0 著作权归作者所有。请勿转载和采集!