Excel COM add sheet after the last sheet Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jekko1976
Posts: 97
Joined: 10 Oct 2014, 07:03

Excel COM add sheet after the last sheet

Post by jekko1976 » 11 Feb 2024, 18:08

Hello,
I would like, using COM, to add a worksheet after the last worksheet.
In the VBA documentation i read that the VB command is:

ActiveWorkbook.Sheets.Add(After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count))

This command "as is" doesn't work in autohotkey.

How could I convert this command to the authotkey COM form?

I leave here the page of the VBA documentation
Thank you

https://learn.microsoft.com/en-us/office/vba/api/excel.sheets.add

User avatar
Datapoint
Posts: 311
Joined: 18 Mar 2018, 17:06

Re: Excel COM add sheet after the last sheet  Topic is solved

Post by Datapoint » 11 Feb 2024, 19:20

Code: Select all

F12::ExcelAddSheetLast()

ExcelAddSheetLast() {
	xlSheets := ComObjActive("Excel.Application").ActiveWorkbook.Sheets
	xlSheets.Add(, xlSheets.Item(xlSheets.Count))
}

jekko1976
Posts: 97
Joined: 10 Oct 2014, 07:03

Re: Excel COM add sheet after the last sheet

Post by jekko1976 » 12 Feb 2024, 02:45

Datapoint wrote:
11 Feb 2024, 19:20

Code: Select all

F12::ExcelAddSheetLast()

ExcelAddSheetLast() {
	xlSheets := ComObjActive("Excel.Application").ActiveWorkbook.Sheets
	xlSheets.Add(, xlSheets.Item(xlSheets.Count))
}
Dear datapoint,
your code is working like a charm and I thank you for the help.
It happends quite often to me that I find the solution in VBA code and then I don't know to how to convert it in "COM/AHK" code.
Does exist some tutorial to operate this conversion?
Thank you so much!
Gabriele

User avatar
Datapoint
Posts: 311
Joined: 18 Mar 2018, 17:06

Re: Excel COM add sheet after the last sheet

Post by Datapoint » 12 Feb 2024, 03:59

Glad to hear it's working for you.
jekko1976 wrote:
12 Feb 2024, 02:45
It happends quite often to me that I find the solution in VBA code and then I don't know to how to convert it in "COM/AHK" code.
Does exist some tutorial to operate this conversion?
There are lots of examples on the forum that can be found with google or the like. Usually you can find something similar to what you are trying to do if you search the AHK site for a specific Excel task. For example, add a worksheet using AHK searching: site:autohotkey.com excel add worksheet

For tutorials on converting VBA to AHK there is this one: viewtopic.php?f=7&t=8978

jekko1976
Posts: 97
Joined: 10 Oct 2014, 07:03

Re: Excel COM add sheet after the last sheet

Post by jekko1976 » 12 Feb 2024, 07:29

thank you, man!
Very useful resources!

Post Reply

Return to “Ask for Help (v1)”