Jump to content
Macro Express Forums

Assigning New Variables on Each Loop


alexlimbert

Recommended Posts

Here is how I would do it:

 

<ASCIIC:95:1:13><ASCIIC:96:1:10><TVAR2:95:01:%T95%%T96%><TVAR2:01:04:C:\Text Document.txt><TMVAR2:21:01:01:001:000:%T95%,><TMVAR2:17:01:00:000:000:C:\temp.txtF><ADFBEG:F10:001:000001:000001:C:\temp.txt><ADFEND><DOFILE:08:NN:C:\temp.txt>><TBOX4:T:1:CenterCenter000278000200:000:T1=%T1%
T2=%T2%
T3=%T3%
T4=%T4%
T5=%T5%>

 

It reformats the file to comma delimited, then sets the variables from it using the ASCII file process command. Maybe someone else can suggest another method?

Link to comment
Share on other sites

Hi I am using the Text File Process feature, and I would like to assign each loop to a new string variable.

For example, I have a text file with three lines (i.e. red, white, and blue) and would like to assign these

colors to variables T1, T2 and T3, respectively.

 

Is there anyway to do this?

 

Thank you.

 

Are you trying to assign the colors to the Text Variables? Or just the words "red", "white", and "blue"?

 

To assign colors, you may need to assign the hex number for the colors to the variable... but I'm not completely

sure that's what you're actually trying to do.

 

If, instead, you have a text file that looks like this:

 

red

white

blue

 

and you simply want your variables set up as: "%T1% = red", "%T2% = white", and "%T3% = blue", then

the answer is simple in an advanced, and sort of complicated way:

 

Variable Set Integer %N1% to 10
Text File Begin Process: "textfileprocess001.txt"
 Variable Set String %T2% "<TVAR2:%N1%:01:%T1%\s>"
 Run Macro in Variable %T2%
 Text Box Display: Text File Process 001
 Variable Modify Integer: Inc (%N1%)
Text File End Process

<IVAR2:01:01:10><BTFBEG:001:000001:000000:C:\Users\Steven\Documents\Macros\Macro Test Files\textfileprocess001.txt><TVAR2:02:01:<TVAR2:%N1%:01:%T1%\s>><RUNMACVAR:2><TBOX4:T:1:CenterCenter000278000200:000:Text File Process 001T10 = %T10%
T11 = %T11%
T12 = %T12%><NMVAR:08:01:0:0000001:0:0000000><BTFEND>

 

First I created a .txt file that is simply:

 

red

white

blue

 

and I saved it to my C: drive (I have a folder specific for testing macros). This macro does the following,

step by step:

 

  • Sets N1 to 10 (this will cause your list to start on %T10%... for some reason I've found that this kind of
    process tends to fail if you start with a single digit Text Variable, and advances into double-digits, so I
    recommend starting no lower than T10).
  • Then you begin the text file process which scans your saved .txt file and places the first line into the
    selected variable (for simplicity's sake I have put mine in T1).
  • Next I create a macro within a variable (this is the part that says:
    Variable Set String %T2%"<TVAR2:%N1%:01:%T1%\s>" )
    There are a lot of resources about this... I don't understand the half of it. However, I do know that this
    process works. I put my macro within the text variable T2, and here is what that means (as much as I
    understand of it, that is):
     
    TVAR2 is code for "T" or Text Variable.
     
    %N1% indicates the value within N1, in this case N1 = 10,
     
    so "TVAR2:%N1%" is code for "%T10%".
     
    :01: = I have no idea
     
    %T1% is the code telling this macro what value to assign to %T10%.
     
    So, in other words, the macro within %T2% will assign the value (contents) of %T1% to %T10%.
    Hence, when I run my process for the first time, T1 will be "red", then the macro within T2 will assign "red" to T10.
     
    At this point, the macro within %T2% is created, but has not yet run (this "macro within T2" could technically
    be set at any previous point in the macro, I just happened to put it immediately prior to the code that runs it...
    the next step).
     
     
  • Now I run the macro that I put within variable T2.
  • At this point, my test macro will open a text box which gives me a running commentary of where I am
    in the process (completely optional, but useful for debugging).
  • Next, I increment the variable N1. Because N1 was originally equal to 10, I want the next item on the list
    to be assigned to T11, not T10... otherwise what was the point, right? So I increment N1 from 10 to 11.
  • Finally, I end the text file process, which is similar to a Repeat End command in that it starts the process
    over on the next line in the text file and will continue doing so until the list has been completed.

 

Clear as mud? If any of this made sense to you... you're either way smarter than me, or I'm better than I

thought. If you need clearer explanations, please don't hesitate to ask. I can try to do better, or (hopefully)

somebody who is way smarter than me can do a better job of explaining it.

Link to comment
Share on other sites

Hi I am using the Text File Process feature, and I would like to assign each loop to a new string variable. For example, I have a text file with three lines (i.e. red, white, and blue) and would like to assign these colors to variables T1, T2 and T3, respectively.

 

Is there anyway to do this?

 

Thank you.

 

Namino's method is elegant and Scasper's is very clever and both have taught me some new things. But I think if I was doing the task from scratch myself I'd have just taken the most direct and (to me) intuitive approach.

 

// RedWhiteBlue - an intuitive but inflexible method
Text File Begin Process: "Test01.txt"
Text File End Process
Text File Begin Process: "Test01.txt"
Text File End Process
Text File Begin Process: "Test01.txt"
Text File End Process
Text Box Display: Result
Macro Return

 

<REM2:RedWhiteBlue - an intuitive but inflexible method><BTFBEG:001:000001:000001:C:\Docs\SUNDRY\Macro Express\Test01.txt><BTFEND><BTFBEG:002:000002:000001:C:\Docs\SUNDRY\Macro Express\Test01.txt><BTFEND><BTFBEG:003:000003:000001:C:\Docs\SUNDRY\Macro Express\Test01.txt><BTFEND><TBOX4:T:1:CenterCenter000278000200:000:ResultT1 = %T1%
T2 = %T2%
T3 = %T3%
><MRETURN>

 

This assumes, of course, that the text file has only a smallish number of lines, say 10 or fewer. In practice I expect the file would be larger and probably of unknown length. In that case I think it should be easy enough to add a Repeat loop containing a Run Macro in Variable command.

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

Namino's method is elegant and Scasper's is very clever and both have taught me some new things.

But I think if I was doing the task from scratch myself I'd have just taken the most direct and (to me)

intuitive approach.

 

<REM2:RedWhiteBlue - an intuitive but inflexible method><BTFBEG:001:000001:000001:C:\Docs\SUNDRY\Macro Express\Test01.txt><BTFEND><BTFBEG:002:000002:000001:C:\Docs\SUNDRY\Macro Express\Test01.txt><BTFEND><BTFBEG:003:000003:000001:C:\Docs\SUNDRY\Macro Express\Test01.txt><BTFEND><TBOX4:T:1:CenterCenter000278000200:000:ResultT1 = %T1%
T2 = %T2%
T3 = %T3%
><MRETURN>

 

This assumes, of course, that the text file has only a smallish number of lines, say 10 or fewer. In practice

I expect the file would be larger and probably of unknown length. In that case I think it should be easy

enough to add a Repeat loop containing a Run Macro in Variable command.

 

--

Terry, East Grinstead, UK

 

Hey Terry,

 

The simplest solution is often the best, and the most intuitive, too. I originally had a macro that was very

similar to the example you give, but it was insanely cumbersome for what I needed. As you point out, the

simple intuitive method isn't practical for longer files. I was processing a file of over 20 lines, each needing

it's own variable. The macro was unbearably long. Discovering the Run Macro in Variable command

smoothed my code out too sweetly not to share it whenever I can :) (I just wish I understood more of the

Direct Editor code. That would, I'm sure, expand my ability to really make my macros sing and dance.)

 

I haven't tested Namino's method yet, but I'm going to very soon. I always prefer elegance to cleverness

in my macros.

Link to comment
Share on other sites

I haven't tested Namino's method yet, but I'm going to very soon. I always prefer elegance to cleverness

in my macros.

 

I like it. I'm not particularly proficient with ACII Characters at this point in my macro-life, so I'm uncomfortable

enough to not attempt them when trying to help other people, and yet this particular solution is, indeed, elegant.

 

I just compared it mentally to the process I mentioned in my last post (the file that required 20+ variables), and

I don't see any reason why this method would not be equally effective.

 

In some ways it would be more effective: You wouldn't need to start at T10, for one thing.

 

However, it may be less effective in other ways: If your list was made up of lines of text which include commas,

for example, I'm not sure how that would effect the process.

 

I guess that could be fixed by adding a bit more code: An "If Variable" statement such as "If Variable %T1%

contains ',' then Replace ',' with '","' in %T1%".

 

Like this:

If Variable %T1% contains ","
 Replace "," with "","" in %T1%
End If

<IFVAR2:1:01:7:,><TMVAR2:21:01:00:000:000:,","><ENDIF>

 

And this would need to be done before replacing %T95% (the CRLF) with "," (otherwise you'd screw up the whole

thing). At least that's how I imagine it would work. Haven't tested my theory yet, but it looks right to me.

 

Heck, you could probably just do away with the "If" statement altogether. If your text already has commas in it,

you probably want them there, so drop the "If" statement and just throw in the Replace code.

Link to comment
Share on other sites

I'm kind of very lost. I would like to do Scasper's suggestion, but I am not sure what is meant:

 

Variable Set Integer %N1% to 10\\\\\\\\\\\\\\\\\\\\\\\\\\Sets N1 to 10 (this will cause your list to start on %T10%) What exactly starts at %T10%?

Text File Begin Process: "textfileprocess001.txt"

Variable Set String %T2% "<TVAR2:%N1%:01:%T1%\s>" \\\\\\\\\\\\\\\\\I don't know direct editing, what does this stand for (or look like) in scripting editor?

Run Macro in Variable %T2%

Text Box Display: Text File Process 001

Variable Modify Integer: Inc (%N1%)\\\\\\\\\\\\\\\\\ Not sure what this is.

Text File End Process

Link to comment
Share on other sites

I'm kind of very lost. I would like to do Scasper's suggestion, but I am not sure what is meant:

 

Variable Set Integer %N1% to 10\\\\\\\\\\\\\\\\\\\\\\\\\\Sets N1 to 10 (this will cause your list to start on %T10%) What exactly starts at %T10%?

Text File Begin Process: "textfileprocess001.txt"

Variable Set String %T2% "<TVAR2:%N1%:01:%T1%\s>" \\\\\\\\\\\\\\\\\I don't know direct editing, what does this stand for (or look like) in scripting editor?

Run Macro in Variable %T2%

Text Box Display: Text File Process 001

Variable Modify Integer: Inc (%N1%)\\\\\\\\\\\\\\\\\ Not sure what this is.

Text File End Process

 

Why not start with one of the simpler methods then?

 

Also, I suggest you study the excellent Help, which covers several of your questions. For example, you say you're not sure what Variable Modify Integer: Inc (%N1%) means. Have you studied the explanation under Help > Index > Variables > Modify Integer Variable ?

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

Yes. I did. The help file is quite helpful. Thanks.

 

By the way, how do you copy and paste code (both Direct and Scripting) into this forumn. What really helped me is copying the direct code into ME, then I clicked Scripting editor and I was able to see what was going on.

 

1. In Macro Express, in Scripting Editor, select all your commands, then right-click and choose Copy Command Text

 

2. In the forum, composing your message, click Insert: CODEBOX from the Quick Access list on the left, then paste in your commands

 

3. In Macro Express, in Direct Editor, copy all your code to the clipboard

 

4. In the forum click Insert: CODEBOX again, then paste in your code.

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

I'm kind of very lost. I would like to do Scasper's suggestion, but I am not sure what is meant:

 

Variable Set Integer %N1% to 10\\\\\\\\\\\\\\\\\\\\\\\\\\Sets N1 to 10 (this will cause your list to start on %T10%) What exactly starts at %T10%?

Text File Begin Process: "textfileprocess001.txt"

Variable Set String %T2% "<TVAR2:%N1%:01:%T1%\s>" \\\\\\\\\\\\\\\\\I don't know direct editing, what does this stand for (or look like) in scripting editor?

Run Macro in Variable %T2%

Text Box Display: Text File Process 001

Variable Modify Integer: Inc (%N1%)\\\\\\\\\\\\\\\\\ Not sure what this is.

Text File End Process

I'll try to clear this up as much as possible, since it's my suggestion that is causing confusion. Some of this is very basic

Macro Express code, though, so please bear with me. This post is going to be rather long, and specific. Terry is right, the

Help files really are a good reference for figuring out the most basic commands. But I also like a challenge and I enjoy

explaining ME in detail, because I often find that as I do so, I learn things I didn't know before (sometimes I suddenly

understand why something works that I didn't understand before, even though I use it).

 

An integral part of ME is the storing of information on "hooks" called variables. There are three basic types of variables in

ME: Text Variables (indicated by a T), Integer Variables (indicated by an N), and Decimal Variables (indicated by a D).

 

Each type of variable can have up to 99 possible hooks per macro. So, you can have Text Variables ranging from

T1 through T99; you can have Integer Variables ranging from N1 through N99; and you can have Decimal Variables

ranging from D1 through D99.

 

Your indicated list consisted of "red white and blue." Assuming the list is in a list format:

 

red

white

blue

 

the macro I built for you would start your list by assigning red to T10, then it would assign white to T11, and finally it

would assign blue to T12.

 

The macro would work for longer lists as well, up to T99 (89 possible lines of text, since it started at T10, not at T1).

 

You don't need to know Direct Editor script to make this work. I don't even begin to understand Direct Editor script. I

just happen to know one particular function for building a macro into a text file that will do what it is you need the macro

to do. I explained that in as much detail as I understood in my previous post.

 

I also provided the Scripting Editor text in that post, so you can see what it looks like in the Scripting Editor (what you

copied into this reply is the scripting editor text).

 

The problem with running a macro from a Text Variable (which is what you have to do to make my macro work), is that

the macro inside the Text Variable has to look like Direct Editor script. It's the only way. I looked for a long time

before figuring that out about a year ago. The thing is, you don't really need to understand Direct Editor script. You just

need to understand what each part of that command means. I tried to explain that in the original response, too. I don't

understand more about it than I stated, but it should be enough for you to duplicate it in your own macro, depending on

your needs.

 

I will repeat the explanation of each step of this macro, and try do be more specific about what each step means.

Step 1:

<IVAR2:01:01:10><BTFBEG:001:000001:000000:C:\Users\Steven\Documents\Macros\Macro Test Files\textfileprocess001.txt><TVAR2:02:01:<TVAR2:%N1%:01:%T1%\s>><RUNMACVAR:2><TBOX4:T:1:CenterCenter000278000200:000:Text File Process 001T10 = %T10%
T11 = %T11%
T12 = %T12%><NMVAR:08:01:0:0000001:0:0000000><BTFEND>

 

But in the scripting editor it will look like this:

Variable Set Integer %N1% to 10
Text File Begin Process: "textfileprocess001.txt"
 Variable Set String %T2% "<TVAR2:%N1%:01:%T1%\s>"
 Run Macro in Variable %T2%
 Text Box Display: Text File Process 001
 Variable Modify Integer: Inc (%N1%)
Text File End Process

Link to comment
Share on other sites

It is not necessary to switch between the Script and Direct Editors in order to copy or paste macro commands in the Direct Editor format. To copy from the Script Editor simply highlight the commands and press Ctrl-c or right click and choose copy. To paste commands into your macro copy them from the source (such as this forum) return to the Script Editor and press Ctrl-v or right click and choose paste.

Link to comment
Share on other sites

An excellent analysis Scasper. I am adding a couple of clarifications, not as criticisms, but to help those who are interested understand more clearly.

 

<TVAR2: indicates that this is a Variable Set String command. The Variable Set String command to set T10 to 'abc' looks like this:

<TVAR2:10:01:abc>

The :10: portion indicates that the value is to be placed in T10.

The :01: portion indicates that this is the 'Set Value Now' option of the Variable Set String command.

:abc>

is the text that will be placed in the T10 variable. Note the separator character just prior to the ending >.

 

In the example:

<TVAR2:%N1%:01:%T1%\s>"

the number of the variable (T10, T11, T12, etc.) is determined by the content of the integer variable %N1%. If %N1% contains 10 then the value will be placed into %T10%, if %N1% contains 11 then %T11% will be used, etc.

%T1% contains the value (red, blue, green, etc.) to assign to %T10%, %T11% or %T12% respectively.

The \s characters are used to indicate placement of the separator character. This (\s) only works for the Run Macro In Variable command.

 

The Variable Set String %T10% from Prompt command looks like this:

<TVAR2:10:02:FefgFFCenter:Center>

Again,<TVAR2: indicates that this is a Variable Set String command.

The :10: portion indicates that the value is to be placed in T10.

The :02: portion indicates that this is the 'Prompt for Value' option of the Variable Set String command.

The remaining portions of the command represent the other options such as Mask Input, Prompt Always On Top, Store CANCEL if Canceled, and the Left and Top settings.

 

Similarly <TVAR2:10:03:> (note the :03:) is the 'Set Value from Clipboard' option of the Variable Set String command.

<TVAR2:10:04:text.txt> (:04:) is the 'Set Value from File' option.

:05: is 'from Program Name' option.

:06: is 'from Window Title'.

:07: is 'from Current Folder Name'.

 

To determine the format of other commands enter the command into the Script Editor and click on the Direct Editor button.

Link to comment
Share on other sites

Kevin, you rock!

 

Thank you for explaining the :01: ... and for all the clarifications! I generally am not happy with "it works

because it works" explanations, so I try hard to figure them out on my own. But I still sometimes reach a point

where my experiments and experiences stop helping me piece the puzzle together without a little nudge from the

people who really know it.

 

Thank you for the nudges! I feel smarter already :)

Link to comment
Share on other sites

Hi, I'm having a lot of trouble with this as well. I have studied the examples and read the many posts for days now, to no avail. I understand part of it.....maybe, but I'm hoping for a little more help from you guys. Hope I'm not Hijacking thread, it is along the same lines.

 

I have an Excel Workbook named Book1

Starting in Column A Row #1

Just to keep it similar, I have:

Red

White

Blue

 

I would like to copy each of these and go over one column and paste each these. Using copying and pasting both using increasing variables, both starting a %T1%.

Could you guys help me with this? Here is what I have so far. It does not work correctly yet! I can't quite figure out how to set what is on the clipboard to %TN1%. Also, I don't have the paste in yet because the top part is wrong.

 

Activate Window: "Microsoft Excel - Book1"

Text Type: <CTRLD>g<CTRLU>A1<ENTER>

Delay 2 Seconds

Variable Set Integer %N1% to 10

Repeat Start (Repeat 3 times)

Clipboard Copy

Variable Set String %T1% from Clipboard

Variable Set String %T1% "<TVAR2:%N1%:01:%T1%\s>"

Run Macro in Variable %T2%

Variable Modify Integer: Inc (%N1%)

Text Type: <ARROW DOWN>

Repeat End

Text Type: <ARROW RIGHT>

Link to comment
Share on other sites

Hi, I'm having a lot of trouble with this as well. I have studied the examples and read the many posts for days now, to no avail. I understand part of it.....maybe, but I'm hoping for a little more help from you guys. Hope I'm not Hijacking thread, it is along the same lines.

 

I have an Excel Workbook named Book1

Starting in Column A Row #1

Just to keep it similar, I have:

Red

White

Blue

 

I would like to copy each of these and go over one column and paste each these. Using copying and pasting both using increasing variables, both starting a %T1%.

Could you guys help me with this? Here is what I have so far. It does not work correctly yet! I can't quite figure out how to set what is on the clipboard to %TN1%. Also, I don't have the paste in yet because the top part is wrong.

In this instance it may be that you are making it harder than it needs to be. While I love the Run Macro in Variable

command and think it's a brilliant solution to specific problems, those particular problems are (in my experience) relatively

rare.

 

What I would probably do, if I were in your shoes, is something more along these lines:

 

First, save your Excel file as a .csv (you can build commands into your macro that will do it for you, but since I don't have

Excel on my computer I don't want to confuse anything by trying to put it into this example macro).

 

I have saved mine as cartwheels.csv and put in into my Macro Test folder on my C: drive. The file is simply this:

Cell A1 contains: Red

Cell A2 contains: White

Cell A3 contains: Blue

 

I want to move these each into its corresponding B cell (i.e. B1 will have Red, etc.)

 

For my macro I want A1 to become blank when B1 receives A1's contents.

 

Here is the Scripting Editor script:

Variable Set String %T1% ""
ASCII File Begin Process: "cartwheels.csv" (Comma Delimited Text )
 Variable Set String %T2% ", "
 Variable Modify String: Append %T1% to %T2%
 Variable Modify String: Append %T2% to Text File
ASCII File End Process

And here is the Direct Editor script

<TVAR2:01:01:><ADFBEG:F10:001:000001:000000:C:\Users\Steven\Documents\Macros\Macro Test Files\cartwheels.csv><TVAR2:02:01:, ><TMVAR2:08:02:01:000:000:><TMVAR2:20:02:00:000:000:C:\Users\Steven\Documents\Macros\Macro Test Files\cartwheels2.csvT><ADFEND>

 

What this macro does is first clears T1 (not really necessary in this macro, though), then it begins an ASCII Process to

retrieve the information, line by line, and puts it into T1.

 

Next I set %T2% to ", " since .csv files are comma delimited. This will ensure that when I append %T1% to %T2% I will

be setting T2 to ", Red" rather than just "Red".

 

Next line appends T1 to T2 (creating ", Red" as explained above) The ", " ensures that when T2 gets saved to a .csv file,

it will be placed in the second column.

 

The next command appends %T2% to the new .csv text file (on the first pass it actually creates the new .csv, but on the

following passes, it will simply add the new information to it). On this line it is imperative to have "Add Trailing CR/LF"

checked.

 

And finally I end the process so that the macro knows to go back and repeat for the next line. When it's done, I have the

original .csv with the A column containing my colors, and a new .csv with those same colors listed in the B column.

 

This macro saves the information into a new .csv file named cartwheels2.csv Macro Express will allow you to rename

this file to replace the original cartwheels.csv, but I didn't want to do that during the test, just because I wanted to make

sure I could run the test over and over without having to manually put everything back the way it was.

 

One last note. It's been a long day for me, and I'm pretty beat, so this may or may not be the best example macro for

doing the job you need done, but after running it a few times, it does do what I programmed it to do, which is what I

understood your needs to be. I'll probably look at it again tomorrow (after a good night's sleep), and see if I missed

my mark. Or you could just tell me I'm way off :).

Link to comment
Share on other sites

Thank you so much for helping me out late on a saturday after long day!

I set this up exactly like you did, but it just seems to sit there and not do anything. When I originally tried to save it as a csv, Excel gives me a message that my worksheet may contain features that are not be compatable with csv.

 

Ultimately though I don't know if this will work because I was really planning using this for a workbook with multiple sheets with lots of info on each sheet. What I do is use ME to take payroll info out of Excel and to put it into our finky accounting software (it's good but difficult to work with sometimes). It is much easier to get it into excel to start with.

 

I have ME copy four cells then toggles to accounting program types them in then toggles back. This actually works pretty well, but I was thinking that it would be far more effeceint to copy everything out of excel first then go to the accounting program and type it all in . This can up to 80 to 100 cells worth of info.

 

Then there is the fact that info from Excel has to be trimed. This all may be more trouble than it is worth. I didn't think it would be so hard when I first started.

 

Thanks again for the help. ME is really a great program! I will be using it a lot.

 

I was hoping to start with a simple but understanable senario then adapt it to a more complicated macro with some controlling "logic" .

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...