3
$\begingroup$

I'm trying to use mathematica to generate some code that I can use in excel.

The goal is to have something like:

IF(BB32="",CA31,0)+IF(BB34="",CA33,0)+IF(BB36="",CA35,0)+IF(BB38="",CA37,0)+IF(BB40="",CA39,0)+IF(BB42="",CA41,0)+ ....

I have written the following code:

Table[StringForm["+IF(BB`1`=\"\",CA`2`,0)", i, i - 1], {i, 44, 118,2}]

This seems to get me really close, but it adds commas between the terms. I could use a find and replace rule to remove the commas, but that would also get rid of the internal commas I want to keep.

I'm not sure what is the best way to go from here.

$\endgroup$
0
6
$\begingroup$

StringForm prints the text for the expression, but it doesn't create strings. Instead, use StringTemplate or TemplateApply. Try this to make the Excel code:

StringRiffle[Table[StringTemplate["IF(BB`1`=\"\",CA`2`,0)"][i, i-1], {i,44,118,2}], "+"]

StringRiffle joins the strings from Table together and inserts "+" between them.

$\endgroup$
1
  • $\begingroup$ Thank you, that worked perfectly, and I learned about StringRiffle to boot. $\endgroup$
    – Wombles
    Dec 30 '20 at 6:23
0
$\begingroup$

An alternative way to use StringRiffle:

StringRiffle[IntegerString[{#, # - 1}] & /@ Range[44, 118, 2], 
  "+", {"IF(BB", "=\"\",CA", ",0)"}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.