Solved

create an incrementing variable name AutoHotKey

Posted on 2016-09-29
5
102 Views
Last Modified: 2016-09-29
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
Comment
Question by:beatified
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Joe Winograd, EE MVE
ID: 41822500
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
 
LVL 2

Author Comment

by:beatified
ID: 41822546
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
 
LVL 52

Accepted Solution

by:
Joe Winograd, EE MVE earned 500 total points
ID: 41822616
> 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
 
LVL 2

Author Comment

by:beatified
ID: 41822620
Thanks so much for helping out a newbie.

Its very appreciated.

Stuart
0
 
LVL 52

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
React or Angular? 6 74
change the windows script file to BAT 10 44
powershell md/mkdir/New-item   -Quiet 10 50
printf performancy 11 45
This article will show, step by step, how to integrate R code into a R Sweave document
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question