To be clear, 255 is a limit on the AppleScript side; Excel allows up to 8192 characters in a formula.
Try this script:
tell application "Microsoft Excel" -- version must be > 2019
tell active sheet
-- fml is 255 characters
set fml to "=LET(
char1,MID(\"ABCDEFGHIJKLMNOPQR\",RANDBETWEEN(1,17),1),
char2,MID(\"ABCDEFGHIJKLMNOPQR\",RANDBETWEEN(1,17),1),
char3,MID(\"ABCDEFGHIJKLMNOPQR\",RANDBETWEEN(1,17),1),
char4,MID(\"ABCDEFGHIJKLMNOPQ\",RANDBETWEEN(1,16),1),
CONCATENATE(char1,char2,char3,char4)
)"
set formula2 of range "D13" to fml
set chars to evaluate name fml
end tell
end tell
and everything works. The formula result will be output to cell D13 and the chars
variable will be set to the result of evaluating the formula.
But try this script:
tell application "Microsoft Excel" -- version must be > 2019
tell active sheet
-- fml is 256 characters
set fml to "=LET(
char1,MID(\"ABCDEFGHIJKLMNOPQR\",RANDBETWEEN(1,17),1),
char2,MID(\"ABCDEFGHIJKLMNOPQR\",RANDBETWEEN(1,17),1),
char3,MID(\"ABCDEFGHIJKLMNOPQR\",RANDBETWEEN(1,17),1),
char4,MID(\"ABCDEFGHIJKLMNOPQR\",RANDBETWEEN(1,17),1),
CONCATENATE(char1,char2,char3,char4)
)"
set formula2 of range "D13" to fml
set chars to evaluate name fml
end tell
end tell
and nothing will be output to Excel and the value of chars
will be missing value
.
(Note: I specify version must be > 2019 just because using a LET
function was the easiest way for me to construct a long formula and LET
is only available in post-2019 versions of Excel.)
But, you can absolutely paste the second formula into the formula bar of Excel and it will work.