Link to home
Start Free TrialLog in
Avatar of beatified
beatifiedFlag for United States of America

asked on

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
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

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
Avatar of beatified

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much for helping out a newbie.

Its very appreciated.

Stuart
You're very welcome, Stuart. Good luck in becoming an AutoHotkey programmer — it's a terrific language. Regards, Joe