1

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)
7
  • Do you want it as a string with the references in there, so it'd look like "=IFERROR(GETPIVOTDATA(...,Pivot!$A$3,..." or do you want that Pivot!$A$3 to actually be the text in that cell? Commented Oct 6, 2016 at 14:12
  • What do you want the formula to look like in the spreadsheet? Commented Oct 6, 2016 at 14:12
  • 1
    Write the desired formula on the sheet itself to make sure it works and post the working formula here with an explanation of which values you want swapped out with variables. The whole formula must be in quotes with the variables outside those quotes concatenated with &. Commented Oct 6, 2016 at 14:12
  • start by getting rid of the two consecutive = = Commented Oct 6, 2016 at 14:13
  • @JohnColeman that's what I meant, that it should be = "=... Commented Oct 6, 2016 at 14:15

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

If it is a working formula and you just want it in a variable as a String, all you need to do is start and end it with double quotes (") and then double all inner quotes (""), like so:

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)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.