Need Help with a Looping a script (beginner)

Get help with using AutoHotkey and its commands and hotkeys
justenuf2bdangerous
Posts: 2
Joined: 30 Oct 2019, 22:55

Need Help with a Looping a script (beginner)

04 Nov 2019, 00:43

Hello and thanks for any you can provide. I have been using AHK for about 2 weeks and have 0 other programming skills. But after much searching and playing around with AHK and finding what has worked for others and incorpating it. I was able to get about 85% of the script that I am trying to write. Here is what I have that works.

I have a script that opens an excel workbook, then stores information in a variable, then allows me to use a hotkey in another program to enter the data cell by cell in another database by repeatedly pressing the control and number pad enter key but I have to hit the tab to get to the next entry point in the database. What I would like to do is use a loop of some kind to automate entering the control and number pad, tab, control number pad, tab series so that I don't have to manually press the control number pad than the tab key to enter the data into the new database.

Here is the script that I have so far, and it works great, but I just can't figure out how to add or where to add the loop command. Also because I am new and have a very limited understanding of how things work and why they work, forgive me for asking any "dumb" follow-up questions about how things work, or if I need any further simplifications. But also thank you for anybody that takes their time to help.

Thanks Chris

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.



; Constants
xlDown := -4121

WorkbookPath := A_ScriptDir "E:\AHK-Studio-master\MACCO FIRST QTR BANK SMTS.xlsx"   ; <-- workbook path 
xlApp := ComObjCreate("Excel.Application")                            ; Creates new Excel
xlApp.Visible := true                                                          ; Make Excel visible
MyWorkbook := xlApp.Workbooks.Open("E:\AHK-Studio-master\MACCO FIRST QTR BANK SMTS.xlsx")                                ; Opens the workbook
Celld743 := xlApp.Cells(850, 1)                                          ; Store a reference to cell starting / cells is the where to start from 743, 1 is A743
LastCell := Celld743.End(xlDown).Offset(0, 4)  ; Store the last cell. .End goes to the end of the sheet and the offset is the number of columns to the right
MyRange := xlApp.Range(Celld743, LastCell)               ; Store a reference to the Range A743:LastCell
CellNumber := 1                                   ; This variable will store the cell number to use
CellCount := MyRange.Cells.Count                            ; Store the count of cells in the range
return

^NumpadEnter::                                                                            ; Ctrl+numpadEnter hotkey
SendRaw % MyRange.Cells(CellNumber).Text      ; Send the current cell specified by 'CellNumber'
CellNumber++                                                     ; Increase 'CellNumber' by one
if (CellNumber > CellCount) {    ; If 'CellNumber' is greater than the total amount of cells...
	MsgBox, 64, Info, Finished. No more cells.                                           ; Done
	CellNumber := 1
}
return                                                                                   ;ends the script
[Mod edit: [code][/code] tags added]
hd0202
Posts: 144
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Need Help with a Looping a script (beginner)

04 Nov 2019, 00:54

Code: Select all

^NumpadEnter::                                                                            ; Ctrl+numpadEnter hotkey
loop {
	SendRaw % MyRange.Cells(CellNumber).Text      ; Send the current cell specified by 'CellNumber'
	CellNumber++                                                     ; Increase 'CellNumber' by one
	if (CellNumber > CellCount) {    ; If 'CellNumber' is greater than the total amount of cells...
		MsgBox, 64, Info, Finished. No more cells.                                           ; Done
		CellNumber := 1
		break
	}
}
Hubert
justenuf2bdangerous
Posts: 2
Joined: 30 Oct 2019, 22:55

Re: Need Help with a Looping a script (beginner)

08 Nov 2019, 00:19

I will give it a try thanks for helping

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Bing [Bot], boiler, johnliem and 239 guests