【VBA】Excel实现批量对单元格替换、加粗文字

使用步骤:

选中你想要处理的单元格或单元格范围。

按 Alt + F8 打开宏对话框,选择 ReplaceCommaWithCommaAndNewLineAndBoldColonText 宏并运行。

效果示例如下图

js 复制代码
Sub ReplaceCommaWithCommaAndNewLineAndBoldColonText()
    Dim cell As Range
    Dim selectedRange As Range
    Dim newText As String
    Dim colonPos As Integer
    Dim ws As Worksheet
    
    ' 获取选中的范围
    Set selectedRange = Application.Selection
    
    ' 获取当前工作表
    Set ws = ActiveSheet
    
    ' 检查是否有选中的单元格
    If Not selectedRange Is Nothing Then
        ' 遍历每个选中的单元格
        For Each cell In selectedRange
            If Not IsEmpty(cell.Value) Then
                ' 替换"、"为"、" + CHAR(10)
                newText = Replace(cell.Value, ":", ":" & vbLf)
                
                ' 更新单元格内容
                cell.Value = newText
                
                ' 设置单元格自动换行
                cell.WrapText = True
                
                ' 查找":"的位置
                colonPos = InStr(cell.Value, ":")
                
                ' 如果找到":",则将":"前面的文字加粗
                If colonPos > 0 Then
                    ' 设置范围
                    ws.Range(cell.Address).Characters(Start:=1, Length:=colonPos - 1).Font.Bold = True
                End If
            End If
        Next cell
    Else
        MsgBox "请先选中一些单元格。", vbExclamation
    End If
End Sub
相关推荐
尤鸟倦1 天前
OneNote table复制到Excel遇到的各种问题
excel
2501_930707782 天前
使用C#代码使用条件格式为 Excel 隔行设置颜色
c#·excel
leihefeng2 天前
PX04-用 Python 读 Excel 画折线图,还能直接插回 Excel 文件
python·excel
yanlaifan2 天前
Excel中指定单元格不能输入内容的方法
excel
babe小鑫3 天前
信息与计算科学专业应届生面试 怎么证明自己能解决业务问题
学习·r语言·excel
Data-Miner3 天前
商品ABC分类分析怎么用AI做?脚本复用+图表可编辑的一次完整实操
人工智能·数据分析·excel
Access开发易登软件4 天前
Access 复杂业务计算,SQL 写在 VBA 还是存储过程?
microsoft·vba·access·access开发
E-iceblue6 天前
Excel 列转行/行列转换全指南:从 4 种常见解法到 Python 批量自动化
python·excel·python库·spire.xls
Data-Miner6 天前
AI处理数据的脚本复用与逻辑复用有什么区别?哪种方式更能沉淀工作成果
数据分析·excel
福兮说6 天前
在浏览器里写一个“够用就好“的 Excel 公式求值器
前端·javascript·excel·编译原理