For Each Variables

Where to find what the variable to use for the current element of a for each loop is?

I am trying to split a string into lines and work on each of those

%Variable%SingleLine% doesn't seem to cut it in this case :wink:

And Peter, this would an awesome spot to mention what the missing variable is :wink:

Thanks :slight_smile:

You need to use the variable name from the For each action.

So in this case your set variable to should be:

AS: %Variable%VarName%

1 Like

OMG that actually worked. I was certain your suggestion was wrong :smiley:

Can you explain the logic behind that though? Where does VarName come from?

Sounds like a placeholder to me, which you would use when explaining how the %Variable%…% construct works

Thanks! :slight_smile:

Take a look here :[]=variable

Hey Sandro,

It looks like you’re not understanding that “VarName” at the top of the action image in your first post is a USER-entered value.

Since you’re using a Lines-In Collection action try changing “VarName” to “lineContents”.

Actions like that one have DEFAULT variable names where appropriate, and that is one of them.

Note that the field can be edited.


The For Each action is incredibly useful, but it can definitely be tricky when you’re first wrapping your mind around how it works. The key thing to understand is that when the macro runs, the For Each action assigns every item in the selected collection(s), one at a time, to whatever variable name you enter in the “For each ______” field (regardless of what the placeholder variable placeholder name may be).

In your Local__code variable case, the For Each action will take each line within it and set it as the content of a variable that is called whatever you enter in the “For each _____” field (VarName by default, but it could also be Line, SingleLine, or even something nonsensical like BlargleHopper). This variable (Line, SingleLine, BlargleHopper, or whatever) will then retain the single line from Local__code as its value for the duration of the loop (i.e. the actions you choose to run for every item in the collection you’re working with), after which For Each will move on to the next line in the collection and repeat the process.
However, if you don’t actually do something with the variable from the “For each _____” field in the loop, then no variables are actually changing from loop to loop, so it won’t produce the results you expect. The warning you saw was telling you to use the same variable name in the For Each actions to ensure that different variable contents were used in every loop.

Hopefully this (and everyone else’s answers) helps shine a bit of light on the logic behind it! If you want a little more information, you can always check the For Each wiki page:

As other mention, then you define the variable name for each line yourself. So in this case you left it to the default VarName.

How could I have missed that??? :scream: :scream: :scream:

Got it now :slight_smile:

Thanks guys!
The speed at which all of you chimed in was awesome :slight_smile:



Don’t feel too bad. I did the exact same thing many years ago.  :smile:


1 Like

And its nice that @peternlewis has made, so KM checks if you use the variable.

1 Like

At least now people know what question to ask.

1 Like

I will extend the text in the alert a little:

A For Each action sets the variable you specified at the start of the action to each entry in the collection(s) and performs the enclosed actions

Perhaps that will help the next person.

1 Like