Solved

create an incrementing variable name AutoHotKey

Posted on 2016-09-29
5
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
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 54

Accepted Solution

by:
Joe Winograd, EE MVE 2015&2016 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 54

Expert Comment

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

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

724 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