weird keystroke result

Hi
was trying to enter a array formula into a cell.

tell application "Microsoft Excel"
	launch
	set formula  of cell "b1" to "=MAX(FREQUENCY(IF(A1:A179=0,ROW(A1:A179)),IF(A1:A179<>0,ROW(A1:A179))))"
	tell application "System Events"
		keystroke "return" using {command down}
	end tell
end tell

font menue and 3 blank scrips came up
any idea why?
manually nothing happens on the keystrokes.
bills

I don’t use MicroSoft products but as far as I know,
(1) you need to specify which is the target document, the target sheet and the target table (I use here the Numbers terminology).

(2) there is no need to issue a keystroke after defining the value of a cell by
set value of someCell to someValue

Yvan KOENIG (VALLAURIS, France) mercredi 11 février 2015 09:48:27

Launch does not open Excel with a document.
So the (unnecessary) keystroke goes somewhere else.

Use activate to get Excel to open with a document.
Like Yvan said, you need to target a specific document, and a specific sheet when there’s more than one.
And ditch the keystroke.

Hi
allistor 993 wrote

I had used the “keystoke” method because that is how you enter an aaray formula manually and I had never entered one before by script.
I had already figured out how to do it. was interested why the “keystroke” brought the results it did?
This works if the workbook sheet is already activated, which it was for the “keystroke” method.

tell application "Microsoft Excel"
	launch
	set formula array of cell "b1" to "=MAX(FREQUENCY(IF(A1:A179=0,ROW(A1:A179)),IF(A1:A179<>0,ROW(A1:A179))))"
end tell

If you activate sheet and workbook, you get a new workbook.

tell application "Microsoft Excel"
	activate
	set formula of cell "b1" of active sheet of active workbook to "=MAX(FREQUENCY(IF(A1:A179=0,ROW(A1:A179)),IF(A1:A179<>0,ROW(A1:A179))))"
	tell application "System Events"
		keystroke "return" using {command down}
	end tell
end tell

Another weird result.
if you run this, with every app shut down, you get 3 scripts and the font menue.

tell application "System Events"
	keystroke "return" using {command down}
end tell

Was just curious to know why it did not work the way I was expecting it to.

Hi.

If you want to simulate hitting Return while holding down the Command key, the code is:

tell application "System Events"
	keystroke return using {command down}
end tell

Note there are no quotes round ‘return’. The way you had it, it was typing the text “return”, equivalent to:

tell application "System Events"
	keystroke "r" using {command down}
	keystroke "e" using {command down}
	keystroke "t" using {command down}
	keystroke "u" using {command down}
	keystroke "r" using {command down}
	keystroke "n" using {command down}
end tell

That could explain the complex behaviour you were seeing. :slight_smile:

Hi
Thanks Nigel,that explains it.
bills