• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

create an incrementing variable name AutoHotKey

I am using AutoHotKey or more specifically a program called Pulover's  Macro Creator to try to create a script to automate some data entry.

I need to create a variable that is auto incremented.

In other words I want to create a some thing like %Var% but with a auto incremented number appended to it each time.
This needs to be part of a loop.

This part of the script will be copying a specific cell in Excel and using this data to create the variable.

Here is how I was doing it but I want to replace this with a loop.

; ********** Activate Excel Window
WinActivate, 9-1-2016 Deposits.xlsx - Excel ahk_exe EXCEL.EXE
Sleep, 333
; ********** Wait for window to be active
WinWaitActive, 9-1-2016 Deposits.xlsx - Excel ahk_exe EXCEL.EXE
Sleep, 333
; ********** Click on Name box to enter sheet and cell reference
ControlClick, Edit1, 9-1-2016 Deposits.xlsx - Excel ahk_exe EXCEL.EXE,, Left, 1,  NA
Sleep, 10
; ********** Send Sheet and cell text
SendRaw, %DEPNUM%!B2
Send, {Enter}
Sleep, 100
; ********** Click on Formula bar
ControlClick, EXCEL<1, 9-1-2016 Deposits.xlsx - Excel,, Left, 1,  NA
Sleep, 10
; ********** Highlight Text in formula bar and then copy that text
Send, {Shift Down}{Home}{Shift Up}{Control Down}{c}{Control Up}{Tab}{Down}{Left}
Sleep, 100
; ********** Assign clipboard contents to Variable name
1APTNUM := Clipboard

I would like to replace this with a loop where 1APTNUM is created by adding an auto incremented number to APTNUM.
For example
I would define APTNUM and it would add the incremented number.
1APTNUM
2APTNUM
3APTNUM
and so on...

This is my attempt and keep in mind it might make no sense at all.

; ********** Defines what row to start on
InputBox, APTSTART, What Line would you like to start on?
; ********** Defines how many records to process
InputBox, RepeatCount, How Many Records are there to process?
; ********** Activate window
WinActivate, 9-1-2016 Deposits.xlsx - Excel ahk_exe EXCEL.EXE
Sleep, 333
; ********** Wait for window to be active
WinWaitActive, 9-1-2016 Deposits.xlsx - Excel ahk_exe EXCEL.EXE
Sleep, 333
; ********** Click on Name box
ControlClick, Edit1, 9-1-2016 Deposits.xlsx - Excel ahk_exe EXCEL.EXE,, Left, 1,  NA
Sleep, 10
; ********** Send text for sheet and Cell
SendRaw, %DEPNUM%!B%APTSTART%
Send, {Enter}
Sleep, 100
; ********** Start Loop
Loop, %RepeatCount%
{
; ********** Click on Formula Bar
    ControlClick, EXCEL<1, 9-1-2016 Deposits.xlsx - Excel,, Left, 1,  NA
    Sleep, 10
; ********** Highlight and copy text
    Send, {Shift Down}{Home}{Shift Up}{Control Down}{c}{Control Up}{Tab}{Down}{Left}
; ********** My horrible attempt at trying to create the auto incremented variable
    APTNUM += Clipboard
; ********** Tells the script to go down one cell
    APTSTART := %APTSTART%+1
}
Sleep, 100

I hope this makes some sense.

Thanks for your help
0
beatified
Asked:
beatified
  • 3
  • 2
1 Solution
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
I haven't looked at your entire script, but here's a working script that creates an incrementing variable name:

NumVars:=5
Loop,%NumVars%
{
  VarNum:=A_Index
  Var_%VarNum%:="This is variable number " . VarNum
  CurrentVar:=Var_%VarNum%
  Msgbox,4096,Variable %VarNum%,Contents=`n%CurrentVar%
}
ExitApp

Open in new window

I used 5 for testing, but test it with whatever value of NumVars you want.

Btw, I don't know your level of AutoHotkey knowledge, but this EE article (and the links in it) may help you:
AutoHotkey - Getting Started

Regards, Joe
0
 
beatifiedAuthor Commented:
Ok I think I partially understand...

It seems that A_Index is defining the incrementing number. Is that correct?

So your creating a %VarNum% which is A_Index so it would = 1,2,3,4 and so on.

Then you create a new variable Var_%VarNum% which is actually text. But I am having trouble understanding the . VarNum portion of it. And in this case Var_%VarNum% = Var_1, Var_2, Var_3 and so on.

Then you create CurrentVar which = Var_%VarNum% and I am guessing this is just returning the Current Variable for the current loop.

Then you create the msgbox and 4096 is likely a size. Variable %VarNum% is the title of the box and Contents= is the text portion and 'n%CurrentVar% is returning the varibale as text in the msgbox.

Am I correct???

Sorry this is pretty new to me.

Thanks for your prompt reply.
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
> It seems that A_Index is defining the incrementing number. Is that correct?

Yes. AutoHotkey has many types of loops, which you should study at the AHK documentation. The built-in variable A_Index contains the number of the current loop iteration. I'm simply using that as a way to implement an incrementing variable name for test purposes.

> So your creating a %VarNum% which is A_Index so it would = 1,2,3,4 and so on.

Yes. I did that for clarity. You could certainly just use the A_Index variable.

> Then you create a new variable Var_%VarNum% which is actually text.

That's the variable name, i.e., Var_1, Var_2, Var_3, etc. The fact that it has text is irrelevant — did that just for illistrative purposes. I could have put anything in those variables via the assignment statement.

> But I am having trouble understanding the . VarNum portion of it.

The dot is the concatenation operator in AHK. So that assignment statement concatenates the literal string ("This is variable number ") with the contents of the variable VarNum.

> Then you create CurrentVar which = Var_%VarNum% and I am guessing this is just returning the Current Variable for the current loop.

It's an assignment statement. It places the contents of the variable Var_1 (then Var_2, then Var_3, etc,) into the variable CurrentVar for use in the MsgBox statement.

> Then you create the msgbox and 4096 is likely a size.

No, it is not a size. Study the MsgBox doc carefully. You'll use it often in AHK scripts.

> Variable %VarNum% is the title of the box and Contents= is the text portion and 'n%CurrentVar% is returning the varibale as text in the msgbox.

Correct.

> Sorry this is pretty new to me.

Take a few days and go through the entire AutoHotkey Beginner Tutorial. It will be time well spent! Regards, Joe
0
 
beatifiedAuthor Commented:
Thanks so much for helping out a newbie.

Its very appreciated.

Stuart
0
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
You're very welcome, Stuart. Good luck in becoming an AutoHotkey programmer — it's a terrific language. Regards, Joe
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now