Excel VBA函数:将计算结果转换为公式字符串
创作时间:
作者:
@小白创作中心
Excel VBA函数:将计算结果转换为公式字符串
引用
CSDN
1.
https://blog.csdn.net/weixin_37942087/article/details/142920363
在工程设计领域,Excel是进行复杂计算的重要工具。为了方便将计算结果以公式形式展示,一位结构设计工程师开发了一个实用的VBA函数,能够将单元格的计算结果转换为完整的公式字符串,并支持位置引用和自定义名称引用。
背景需求
在工程设计工作中,Excel被广泛用于各种计算任务。为了便于记录和展示计算过程,有时需要将单元格的计算结果转换为公式字符串显示。具体需求包括:
- 将单元格的计算结果转换为公式字符串
- 支持位置引用(如A1、B2等)
- 支持自定义名称引用
- 如果最外层使用了ROUND函数,需要隐去该函数
函数实现
下面是一个满足上述需求的VBA函数实现:
Public Function xqzde(ans As Range) As String
' de for display equation
Dim formulastring As String
Dim innerformula As String
' 读取单元格(公式形式)
formulastring = ans.Formula
'1. 替换常用自定函数π以及绝对位置引用符号
formulastring = Replace(formulastring, "$", "")
formulastring = Replace(formulastring, "Pi()", "3.14")
formulastring = Replace(formulastring, "pi()", "3.14")
formulastring = Replace(formulastring, "PI()", "3.14")
'2. 识别外层round函数,有则删除
Dim ws As Worksheet
Set ws = ActiveSheet
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True '全局匹配
regex.IgnoreCase = True '忽略大小写
regex.Pattern = "round\((.*)\)$"
Set matches = regex.Execute(formulastring)
If matches.Count > 0 Then
innerformula = matches(0).SubMatches(0)
commaposition = InStr(innerformula, ",")
innerformula = "=" & Left(innerformula, commaposition - 1)
Else
innerformula = formulastring ' 如果没有找到 round 函数,返回原始公式
End If
formulastring = innerformula
'3. 识别公式中的自定义名称并进行替换
Dim nm As Name
Dim namesArray() As Variant
Dim prefixpos As Integer
Dim cusname As String
Dim cellvalue As String
Dim temp As Variant
Dim isSorted As Boolean
ReDim namesArray(1 To ws.Names.Count, 1 To 2)
i = 1
' 3.1 遍历名称管理器中的所有名称
For Each nm In ws.Names
If nm.Parent.Name = ws.Name Then
namesArray(i, 1) = nm.Name
namesArray(i, 2) = Len(nm.Name)
i = i + 1
End If
Next nm
ReDim Preserve namesArray(1 To i - 1, 1 To 2)
' 使用冒泡排序算法按名称长度排序(先长后短)
For j = LBound(namesArray, 1) To UBound(namesArray, 1) - 1
isSorted = True
For i = LBound(namesArray, 1) To UBound(namesArray, 1) - 1
If namesArray(i, 2) < namesArray(i + 1, 2) Then
temp = namesArray(i, 1)
namesArray(i, 1) = namesArray(i + 1, 1)
namesArray(i + 1, 1) = temp
temp = namesArray(i, 2)
namesArray(i, 2) = namesArray(i + 1, 2)
namesArray(i + 1, 2) = temp
isSorted = False
End If
Next i
If isSorted Then Exit For
Next j
' 处理排序后的名称
For i = LBound(namesArray, 1) To UBound(namesArray, 1)
Set nm = ThisWorkbook.Names(namesArray(i, 1))
If nm.Parent.Name = ws.Name Then
prefixpos = InStr(nm.Name, "!")
If prefixpos > 0 Then
cusname = Mid(nm.Name, prefixpos + 1)
Else
cusname = nm.Name
End If
cellvalue = CStr(Range(nm.RefersTo).Value)
formulastring = Replace(formulastring, cusname, cellvalue)
End If
Next i
'4. 识别公式中的位置引用并进行替换
regex.Pattern = "([A-Z]+\d+)"
Set matches = regex.Execute(formulastring)
foundformula = formulastring
For i = 0 To matches.Count - 1
Set cellRef = ws.Range(matches(i).SubMatches(0))
If IsNumeric(cellRef.Value) Then
cellvalue = CStr(cellRef.Value)
Else
cellvalue = """" & cellRef.Value & """"
End If
foundformula = Replace(foundformula, matches(i).Value, cellvalue)
Next i
xqzde = foundformula & "="
End Function
使用示例
假设E3单元格包含一个复杂的计算公式,我们可以在A3单元格中使用这个函数来显示其公式字符串:
这个函数可以大大简化计算书的制作过程,提高工作效率。
总结
这个VBA函数通过字符串处理、正则表达式匹配和名称管理器操作,实现了将Excel单元格计算结果转换为公式字符串的功能。对于需要频繁使用Excel进行复杂计算的专业人士来说,这个函数具有很高的实用价值。
热门推荐
如何避免错误的炒房行为?这种错误行为会带来哪些后果?
减少信访流程管控力度后如何确保问题得到有效解决
如何梳理学校的理念文化
医院病房设计如何从细节入手,真正做到以人为本?
《上古卷轴5:天际》种族强弱排名全解析
计算机视觉入门:理解基础概念和应用
外科护理:断肢(指)再植病人的护理指南
如何有效建立项目风险监控体制?
连日饮酒后险些失明,抽丝剥茧找出致病元凶——假酒中毒
《原神》的历届「海灯节」各有什么不同?哪个画面给你的印象最深刻?
来文登,Get春分的由来
一文详解:什么是新加坡“税务居民”,成为新加坡税收居民有何优势
新加坡餐饮业征收GST规则详解:啤酒销售税率、申报流程及合规要求
如何确定有效的理财方法?这个方法怎样实现财务目标?
羽毛球冠军林丹,为什么是“神”,而不是人
林丹,他的职业生涯充满传奇色彩,今天就探寻他每一个高光时刻
Excel按年龄段查询提取数据的三种方法
13 种最适合榨汁的橙子
柳蛳蛳螺蛳粉有不辣的吗?辣度口味选择丰富,满足不同口味需求
腹股沟拉伤康复指南:从症状识别到渐进式训练方案
腹股沟拉伤的正确处理方法
概率论基础学习:从随机变量到期望方差
利用AI技术优化你的写作流程
企业提交重整申请后,法院多久能受理成功?
研究证实:类风湿性关节炎遗传易感性与支气管扩张症风险增加存在因果关联
又要掀翻全球市场?日元套利交易“韭菜旺” 日本央行伸向“加息刀”
常用免疫组化评分方法介绍
牙疼导致头疼的厉害怎么办
化工侵权包装与品牌包装的区别及维权指南
数据管理需求分析指南:从目标设定到实施监控