VBA 行内容相同时字体变灰

1. 效果

2. 实现代码

vbscript 复制代码
Option Explicit

Sub sbSameFontGrey()
    Dim i, j As Long
    Dim rng As Range
    Dim maxUsedRow, maxUsedCol As Long
    Dim ws As Worksheet: Set ws = ActiveSheet

On Error GoTo ErrProcess

    If TypeName(Selection) <> "Range" Then
        MsgBox "Please select a cell range first!", vbExclamation
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    
    maxUsedRow = ws.UsedRange.Row + ws.UsedRange.Rows.Count - 1
    maxUsedCol = ws.UsedRange.Column + ws.UsedRange.Columns.Count - 1
    
    For j = Selection.Column To Selection.Column + Selection.Columns.Count - 1
        For i = Selection.Row + 1 To Selection.Row + Selection.Rows.Count - 1
            Set rng = Cells(i, j)
            rng.Font.Color = RGB(0, 0, 0) 'Color:Black
            If rng.Value = rng.Offset(-1).Value Then rng.Font.Color = RGB(217, 217, 217) 'Color:Grey
            If i > maxUsedRow Then Exit For
        Next
        If j > maxUsedCol Then Exit For
    Next
    
    Application.ScreenUpdating = True
    Exit Sub
ErrProcess:
    Application.ScreenUpdating = True
    MsgBox Err.Description
End Sub
相关推荐
VBA633713 天前
AI批量PDF数据抓取工具
vba
Access开发易登软件16 天前
我全新开发了一套 Access 通用 Excel 导入工具,现在开源了
数据库·microsoft·excel·mfc·vba·access·access开发
VBA633716 天前
VBA之Word应用第五章第九节 Range对象的方法(三)
vba
VBA633724 天前
VBA 64位API声明语句第025讲
vba
Access开发易登软件1 个月前
Access 导入 Markdown 表格:把文档里的数据写进表
前端·html·vba·markdown·access开发
zhongwei2281 个月前
vba将一个文件夹的内容复制到另外一个文件夹的函数,若存在则是否覆盖的参数。复制前检查源文件夹是否存在。若目标文件夹不存在,则新建
前端·vba
Access开发易登软件1 个月前
Access 复杂业务计算,SQL 写在 VBA 还是存储过程?
microsoft·vba·access·access开发
SunnyDays10112 个月前
如何使用 Python 添加和编辑 Excel VBA 宏(无需安装 Excel)
开发语言·python·excel·vba
taller_20002 个月前
【005】PiE 与传统 VBA 的核心区别
python·vba·pie·py