简体   繁体   中英

Excel VBA Formula Inside a String

Trying to put a formula inside a string and can't get it working. Can someone help me turn this into a string? I know I need to do ""&"" in places, I just don't know where....

Dim form As String
form = =IFERROR(GETPIVOTDATA("[Measures].[Sum of Amount]",Pivot!$A$3,"[qAllData].[Year]","[qAllData].[Year].&[2.016E3]","[qAllData].[Month Name]","[qAllData].[Month Name].&["&$B9&"]","[qAllData].[PRODUCT_CODE]","[qAllData].[PRODUCT_CODE].&["&C$3&"]","[qAllData].[Product Name]","[qAllData].[Product Name].&["&C$4&"]")-C8-C7-C6-C5,0)

If your formual is verbatim what you have in the spreadsheet but you want to insert it with VBA then just:

Dim form As String
form = "=IFERROR(GETPIVOTDATA(""[Measures].[Sum of Amount]"",Pivot!$A$3,""[qAllData].[Year]"",""[qAllData].[Year].&[2.016E3]"",""[qAllData].[Month Name]"",""[qAllData].[Month Name].&[""&$B9&""]"",""[qAllData].[PRODUCT_CODE]"",""[qAllData].[PRODUCT_CODE].&[""&C$3&""]"",""[qAllData].[Product Name]"",""[qAllData].[Product Name].&[""&C$4&""]"")-C8-C7-C6-C5,0)"

This simply replaced each " by "" and wrapped the whole thing in quotes. VBA accepts it as a valid formula, though I am not sure that this is what you really want.

如果它是一个工作公式,你只需要在一个变量中作为一个String ,你需要做的就是用双引号( " )开始和结束它,然后加倍所有内引号( "" ),如下所示:

form = "=IFERROR(GETPIVOTDATA(""[Measures].[Sum of Amount]"",Pivot!$A$3,""[qAllData].[Year]"",""[qAllData].[Year].&[2.016E3]"",""[qAllData].[Month Name]"",""[qAllData].[Month Name].&[""&$B9&""]"",""[qAllData].[PRODUCT_CODE]"",""[qAllData].[PRODUCT_CODE].&[""&C$3&""]"",""[qAllData].[Product Name]"",""[qAllData].[Product Name].&[""&C$4&""]"")-C8-C7-C6-C5,0)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM