zoukankan      html  css  js  c++  java
  • 在excel中给函数、公式结果加格式,如颜色,字体

    有时候有这样一种场景,excel的公式/函数结果都是没有样式的,一般情况下只能整体的改变单元格的样式,不能只修改单元格内部分文本的样式。由于excel本身的限制,改需求并不能通过公式或VBA函数直接实现。

    如下提供一个替代方案:

    1. 将公式写在自定义的单元格内,计算期望的结果

    2. 将该列隐藏起来

    3. 在VBA中写入如下代码,将其值格式化后写在后一列内

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i&, m&
    Application.EnableEvents = False
    Set targetcolumn = Me.UsedRange
    For Each t In targetcolumn
        If t.Column = 7 Then
            If t.Value <> "" Then
                m = Len(t.Value)
                For i = 1 To m
                    If Mid(t.Value, i, 4) = "Lion" Then
                        Set nextcol = Cells(t.Row, t.Column + 1)
                        nextcol.Value = t.Value
                        With nextcol.Characters(1, i - 1).Font
                            .ColorIndex = 0
                        End With
                        With nextcol.Characters(i, 4).Font
                            .ColorIndex = 3
                        End With
                        With nextcol.Characters(i + 4, m - i).Font
                            .ColorIndex = 0
                        End With
                        Exit For
                    End If
                Next
            End If
        End If
    Next t
    Application.EnableEvents = True
    End Sub

    该代码写在worksheet里。

    4. 最终效果:

  • 相关阅读:
    多线程
    python 面向对象
    selenium 安装 以及相关环境
    pyquery 库的方法
    Python 面向对象的补充
    python 面向对象
    想造轮子的时候,ctrl+f一下
    C#三层开发做学生管理系统
    C# 我是个传奇的 using
    啦啦啦 啦啦 啦 啦 啦 啦啦 啦 啦 啦
  • 原文地址:https://www.cnblogs.com/crazyghostvon/p/vbaformatformula.html
Copyright © 2011-2022 走看看