如何根据表名称和具有IDS的ID替换公式文本,以固定的列名称
我必须根据所需的ID替换公式文本(始终在B列中),具体取决于所引用的表格名称,如下所示,附有附件的Excel的屏幕截图(所需的输出以黄色突出显示),
例如。公式:= node!a2 * comp!a2
新单元格中所需的输出:[n1] * [C1]
随附的屏幕截图,
< img src =“ https://i.sstatic.net/7epbj.png” alt =“在此处输入图像说明”>
编辑 - 我尝试了以下事情, 1]在结果表上添加了1个按钮 2]单击它,称为宏的代码如下
Sub Button1_Click()
Dim formulaVal As String
Dim str1 As String
Dim str12 As String
formulaVal = Range("A2").Formula
Debug.Print formulaVal
'MsgBox Range("J2").Value2
str1 = Replace(formulaVal, "!A", "!B")
Debug.Print str1
'ActiveSheet.Range("E2").Formula = strl
ActiveSheet.Range("E2").Formula = "=Node!B2*Comp!B2"
'MsgBox Range("u2").Value
strl2 = Range("E2").Formula
Debug.Print strl2
End Sub
3]在按钮单击“获取所需公式”之后,它显示错误数据类型不匹配,因为引用的数据是字符串,该字符串与公式
不兼容,请建议替代解决方案,如果任何
I have to replace formula text by required IDs(always in column B) depending upon sheet names referred, as example given below also Screenshot of excel attached herewith (required output is highlighted with yellow color)
eg. formula: =Node!A2 * Comp!A2
required output in new cell: [n1] * [c1]
attached screenshots,
EDIT -
I have tried following things,
1] Added 1 button on Result sheet
2] On click of it called macro having code as below
Sub Button1_Click()
Dim formulaVal As String
Dim str1 As String
Dim str12 As String
formulaVal = Range("A2").Formula
Debug.Print formulaVal
'MsgBox Range("J2").Value2
str1 = Replace(formulaVal, "!A", "!B")
Debug.Print str1
'ActiveSheet.Range("E2").Formula = strl
ActiveSheet.Range("E2").Formula = "=Node!B2*Comp!B2"
'MsgBox Range("u2").Value
strl2 = Range("E2").Formula
Debug.Print strl2
End Sub
3] After button clicking getting required formula in cell but it is showing error data type mismatch as referred data is as string which is not compatible with formula
Please suggest alternate solution if any
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在B2中使用此公式并将其拉下来:
You can use this formula in B2 and pull it down:
如果您有Windows环境&amp; Office 365您可以使用以下内容:
FilterXml()用于在每个
*
上将文本分开。拆分的结果用于获取第一个字符(并将其更改为较低的情况),并在
!a
后面找到字符串,这是行号(并从中提取1)。然后textjoin()加入计算的字符和行号,并在每个字符之间放置
*
。ps如果
a1
是实际公式,而不是替换a1
,form> formulatext(a1)
(有关FilterXml的更多信息可以在JVDV的这篇文章中找到: https://stackoverflow.com/a/61837697
)这仅提供了现在问题的部分答案。答案是基于:

If you have a Windows environment & Office 365 you could use the following:
FILTERXML() is used to split the text at each
*
.Thee result of the split is used to get the first Character (and change it to lower case) and to find the string behind
!A
, which is the row number (and substract 1 from it).Then TEXTJOIN() joins the calculated character and row number and places a
*
between each.PS if
A1
is an actual formula than replaceA1
withFORMULATEXT(A1)
(More info on FILTERXML can be found in this post by JvdV: https://stackoverflow.com/a/61837697)
EDIT: I see the question was updated and this only provides an answer to part of the question now. Answer was based on :
