Page 1 of 1

Go to a specific sheet on excel

Posted: 06 Sep 2022, 12:49
by Empaque
Im writing a code that needs to change constantly from vairious excel sheets, i found this code to activate an specified sheet in the active book of excel.

Code: Select all

F1::
Xl := ComObjActive("Excel.Application") ;creates a handle to your currently active excel sheet
Xl.Sheets("Sheet1").Activate
return
My question is, how can i use a variable to change each sheet.

My idea was this code:

Code: Select all

F2::
count=1
Xl := ComObjActive("Excel.Application") ;creates a handle to your currently active excel sheet
Xl.ActiveSheet.Range("b1").select
send ^c
#HotkeyModifierTimeout, 100
sleep 500
StringReplace, clipboard, clipboard, `r`n,, all

Tipo=%Clipboard%
WinActivate, PORMEM_ADE1
Xl := ComObjActive("Excel.Application") ;creates a handle to your currently active excel sheet
Xl.Sheets("%Tipo%").Activate
return
I got an error in the last line before return, i guess its only the way i want to introduce the variable bue i dont find thw way to do it correctly, i´ll be so gretfull if some one can help me.

Re: Go to a specific sheet on excel  Topic is solved

Posted: 06 Sep 2022, 13:14
by Heezea
Try Xl.Sheets(Tipo).Activate

Recall that when a parameter is in brackets, you're sending the value of the parameter so you don't need the " or the %. Xl.Sheets("Tipo").Activate would activate a sheet named Tipo.

Similarly but opposite, when not in brackets, you do need the %

Code: Select all

MsgBox, % Tipo " is the value of Tipo.`nWhen the % is in the front, it's similar to a parameter of a function."
MsgBox, Tipo is the name of the variable, not the contents.
MsgBox, %Tipo% is the value of Tipo.
It's probably worth reading the documentation on variable expression.

Re: Go to a specific sheet on excel

Posted: 06 Sep 2022, 13:22
by Empaque
Thnaks a lot, that now works.