如何根据表名称和具有IDS的ID替换公式文本,以固定的列名称

发布于 2025-01-31 14:49:05 字数 1118 浏览 1 评论 0原文

我必须根据所需的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,

enter image description here

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2025-02-07 14:49:05

您可以在B2中使用此公式并将其拉下来:

="n"&VALUE(MID(FORMULATEXT(A2),8,FIND("*",FORMULATEXT(A2))-8))-1 & "*c" & VALUE(MID(FORMULATEXT(A2),FIND("Comp!A",FORMULATEXT(A2))+6,10))-1

You can use this formula in B2 and pull it down:

="n"&VALUE(MID(FORMULATEXT(A2),8,FIND("*",FORMULATEXT(A2))-8))-1 & "*c" & VALUE(MID(FORMULATEXT(A2),FIND("Comp!A",FORMULATEXT(A2))+6,10))-1
糖粟与秋泊 2025-02-07 14:49:05

如果您有Windows环境&amp; Office 365您可以使用以下内容:

=LET(split,FILTERXML("<a><b>"&SUBSTITUTE(SUBSTITUTE(FORMULATEXT(A1),"=",""),"*","</b><b>")&"</b></a>","//b"),
        char,LOWER(LEFT(split,1)),
        row,MID(split,FIND("!A",split)+2,LEN(split))-1,
"="&TEXTJOIN("*",1,char&row))

FilterXml()用于在每个*上将文本分开。
拆分的结果用于获取第一个字符(并将其更改为较低的情况),并在!a后面找到字符串,这是行号(并从中提取1)。
然后textjoin()加入计算的字符和行号,并在每个字符之间放置*

ps如果a1是实际公式,而不是替换a1form> formulatext(a1)

(有关FilterXml的更多信息可以在JVDV的这篇文章中找到: https://stackoverflow.com/a/61837697

)这仅提供了现在问题的部分答案。答案是基于:

If you have a Windows environment & Office 365 you could use the following:

=LET(split,FILTERXML("<a><b>"&SUBSTITUTE(SUBSTITUTE(FORMULATEXT(A1),"=",""),"*","</b><b>")&"</b></a>","//b"),
        char,LOWER(LEFT(split,1)),
        row,MID(split,FIND("!A",split)+2,LEN(split))-1,
"="&TEXTJOIN("*",1,char&row))

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.

enter image description here

PS if A1 is an actual formula than replace A1 with FORMULATEXT(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 :
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文