【VBA】Excel指定单元格范围内字体设置样式,处理导出课表单元格

提示词:输出一个vba,对选中单元格做以下处理:

  • 搜索"周"字,将"周"字前面直到符号的字体"/"加粗变红;
  • 搜索"分校区",将"分校区"后面直到符号"/"的字体加粗变蓝;
  • 搜索到第一个符号"/",对该符号前面的字体设置为11号大小

效果如下图

样式处理代码

bash 复制代码
Sub FormatCellTextBasedOnCriteria()
    Dim cell As Range
    Dim selectedRange As Range
    Dim cellValue As String
    Dim zhouPos As Integer
    Dim slashPos As Integer
    Dim fenXiaoQuPos As Integer
    Dim startOfText As Integer
    Dim endOfText 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
                cellValue = cell.Value
                
                ' 查找第一个"/"的位置
                slashPos = InStr(cellValue, "/")
                
                If slashPos > 0 Then
                    ' 设置第一个"/"前面的文字为11号大小
                    startOfText = 1
                    endOfText = slashPos - 1
                    ws.Range(cell.Address).Characters(Start:=startOfText, Length:=endOfText - startOfText + 1).Font.Size = 11
                End If
                
                ' 处理"周"
                zhouPos = InStr(cellValue, "周")
                
                ' 只要找到"周",就继续处理
                While zhouPos > 0
                    ' 查找"/"的位置(从"周"前面开始往前查找)
                    slashPos = RevInStr(cellValue, "/", zhouPos - 1)
                    
                    If slashPos > 0 Then
                        ' 计算需要加粗变红的文本范围
                        startOfText = slashPos + 1
                        endOfText = zhouPos + 1 ' 包括"周"字本身
                    Else
                        ' 如果没有找到"/",则从开头到"周"字
                        startOfText = 1
                        endOfText = zhouPos + 1 ' 包括"周"字本身
                    End If
                    
                    ' 设置字体加粗并变红
                    ws.Range(cell.Address).Characters(Start:=startOfText, Length:=endOfText - startOfText + 1).Font.Bold = True
                    ws.Range(cell.Address).Characters(Start:=startOfText, Length:=endOfText - startOfText + 1).Font.ColorIndex = 3 ' 3 对应红色
                    
                    ' 查找下一个"周"的位置
                    zhouPos = InStr(zhouPos + 1, cellValue, "周")
                Wend
                
                ' 处理"分校区"
                fenXiaoQuPos = InStr(cellValue, "分校区")
                
                ' 只要找到"分校区",就继续处理
                While fenXiaoQuPos > 0
                    ' 查找"/"的位置(从"分校区"后面开始往后查找)
                    slashPos = InStr(fenXiaoQuPos + 3, cellValue, "/")
                    
                    If slashPos > 0 Then
                        ' 计算需要加粗变蓝的文本范围
                        startOfText = fenXiaoQuPos
                        endOfText = slashPos - 1
                    Else
                        ' 如果没有找到"/",则从"分校区"到字符串结尾
                        startOfText = fenXiaoQuPos
                        endOfText = Len(cellValue)
                    End If
                    
                    ' 设置字体加粗并变蓝
                    ws.Range(cell.Address).Characters(Start:=startOfText, Length:=endOfText - startOfText + 1).Font.Bold = True
                    ws.Range(cell.Address).Characters(Start:=startOfText, Length:=endOfText - startOfText + 1).Font.ColorIndex = 5 ' 5 对应蓝色
                    
                    ' 查找下一个"分校区"的位置
                    fenXiaoQuPos = InStr(fenXiaoQuPos + 6, cellValue, "分校区")
                Wend
            End If
        Next cell
    Else
        MsgBox "请先选中一些单元格。", vbExclamation
    End If
End Sub

' 自定义的反向查找函数
Function RevInStr(searchString As String, findString As String, Optional startPos As Integer = -1) As Long
    Dim i As Long
    Dim lenFind As Long
    
    lenFind = Len(findString)
    
    If startPos = -1 Then
        startPos = Len(searchString)
    End If
    
    For i = startPos To 1 Step -1
        If Mid(searchString, i, lenFind) = findString Then
            RevInStr = i
            Exit Function
        End If
    Next i
    
    RevInStr = 0
End Function

补充一个 换行 代码

bash 复制代码
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
           
            End If
        Next cell
    Else
        MsgBox "请先选中一些单元格。", vbExclamation
    End If
End Sub
相关推荐
百数平台19 小时前
百数表格知识库配置指南:Excel/CSV 上传、表结构字段类型与 NL2SQL 查询全说明
excel
葡萄城技术团队20 小时前
在 DSH 中直接查看和编辑 Excel:dsh-spreadjs-editor 开源插件发布
excel
开开心心就好21 小时前
视频里的图片怎么提取?双击一下就导出
java·前端·人工智能·spring·智能手机·intellij-idea·excel
葡萄城技术团队1 天前
从 AI 生成到在线预览:dsh-spreadjs-editor 补齐 DSH 的 Excel 任务闭环
excel
泡海椒1 天前
JQuick-Excel 实战:用 STYLE 配置单元格与区域样式
开发语言·python·excel
优选资讯1 天前
标签打印软件怎么对接 Excel 数据
前端·excel
泡海椒2 天前
JQuick-Excel 实战:FORMAT 管理日期与数字的 Excel 显示
开发语言·python·excel
AIGS0012 天前
从814张表到250个本体:本体建模在做什么
数据库·excel·erp·数据中台·智能问数·本体语义·企业大脑
泡海椒2 天前
JQuick-Excel VALIDATION 实战:用作用范围约束 Excel 导入校验
开发语言·python·excel
jason.zeng@15022072 天前
(八)现有架构上新增一个通用Excel导出工具
python·架构·langchain·excel·llama