【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
相关推荐
jaysee-sjc26 分钟前
【苍穹外卖】Day12:工作台数据看板与 Excel 报表导出
java·开发语言·数据库·spring boot·spring·微信·excel
ms365copilot5 小时前
Copilot 代理开始进入Word、Excel、PowerPoint,不再只是“问一句答一句”
word·excel·copilot
wuyk5555 小时前
PyQt6-第14讲-数据库与Excel双向同步
数据库·python·ui·excel·pyqt
qq_364682156 小时前
AI如何将录音内容自动整理成Excel清单
人工智能·excel
Logic1017 小时前
Excel信息+照片文件夹,自动合成word格式化文档
word·excel·办公自动化·邮件合并·office技巧·自动插入图片·插入域
Data-Miner1 天前
一个省大模型积分的 Excel 数据处理方案:脱敏生成逻辑,脚本复用把 token 降到 0
人工智能·windows·excel
Logic1012 天前
表格智能记账3 销售与库存智能联动,自动标红库存预警
excel·条件格式·excel技巧·vlookup·sumif
技术小甜甜2 天前
【Copilot】能选 Claude 和 GPT-6 了,Excel 分析该交给谁
gpt·excel·copilot·claude·microsoft365·gpt-6
小白今天也很酷3 天前
MCXN947更新板载DAP_Link固件的方法
c语言·python·单片机·ubuntu·excel·1024程序员节
ratel️4 天前
小墨析 · Office Agent 日常办公不烧心
python·langchain·word·excel·fastapi·js·ppt