Function or similar to execute commands Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DanielMendis
Posts: 11
Joined: 22 Jun 2022, 08:30

Function or similar to execute commands

Post by DanielMendis » 30 Jun 2022, 06:54

Hi All

I am trying to define a function that incorporates some simple commands so that the cursor jumps to a certain cell in Excel. Here is my script:

Code: Select all


GoToCell(x){
WinActivate, Excel, 
Send, {CTRLDOWN}g{CTRLUP}
WinWait, Gehe zu, 
Send, x{ENTER}
Sleep, 500
}
Return 

#^8::
GoToCell (B3)
Return 
So as you can see I defined the function GoToCell and try to call it with the value B3 (which should replace the "x" in the function). I think this is probably not the right way to do it so any help would be very appreciated!
Thanks Daniel

User avatar
boiler
Posts: 16960
Joined: 21 Dec 2014, 02:44

Re: Function or similar to execute commands  Topic is solved

Post by boiler » 30 Jun 2022, 07:05

You have two lines with syntax issues. This line would need % around the variable x:

Code: Select all

Send, x{ENTER}
And this line cannot have a space between the function name and parenthesis, and B3 would need to be in quotation marks:

Code: Select all

GoToCell (B3)
The first Return line does nothing where you have it and isn’t needed anywhere in this script.

To understand when and how to use expressions and legacy syntax, see the documentation on Expressions, Legacy Syntax, and Expressions vs Legacy Syntax.

User avatar
mikeyww
Posts: 26942
Joined: 09 Sep 2014, 18:38

Re: Function or similar to execute commands

Post by mikeyww » 30 Jun 2022, 07:09

Code: Select all

#IfWinExist ahk_exe EXCEL.exe
#^8::
WinActivate
excelGoToCell("B3")
Return
#IfWinExist

excelGoToCell(cell) {
 ComObjActive("Excel.Application").Range(cell).Select
}

User avatar
DanielMendis
Posts: 11
Joined: 22 Jun 2022, 08:30

Re: Function or similar to execute commands

Post by DanielMendis » 30 Jun 2022, 07:38

mikeyww wrote:
30 Jun 2022, 07:09

Code: Select all

excelGoToCell(cell) {
 ComObjActive("Excel.Application").Range(cell).Select
}
Thanks mikeyww, this looks interesting :-) I have to examine this a little bit more to understand what you are doing but I am sure it is a good way.

User avatar
boiler
Posts: 16960
Joined: 21 Dec 2014, 02:44

Re: Function or similar to execute commands

Post by boiler » 30 Jun 2022, 07:47

It's a better way because it doesn't rely on the Excel window being active and keystrokes being sent/received, which is not 100% reliable.

Post Reply

Return to “Ask for Help (v1)”