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. 最终效果:

  • 相关阅读:
    高维协方差矩阵估计
    互信息
    投资组合模型
    R语言
    sklearn
    Python学习
    swagger使用过程中遇到的坑
    mysql杂文
    2018狗年,半年报
    Springboot 手动搭建项目 --redis配置&日志完善+用户名
  • 原文地址:https://www.cnblogs.com/crazyghostvon/p/vbaformatformula.html
Copyright © 2011-2022 走看看