Excel: creating an array with n times a constant

两盒软妹~` 提交于 2021-02-10 12:33:07

问题


I have been looking around for a while but unable to find an answer to my question.

In Excel, what compact formula can I use to create an array made up of a single element repeated n times, where n is an input (potentially hard-coded)?

For example, something that would look like this (the formula below does not work but gives an idea of what I am looking for):

{={"Constant"}*3}

Note: I am not looking for a VBA-based solution.


回答1:


One could concatenating 1:n empty cells to the "Constant" for creating a string array having n items "Constant":

"Constant"&INDEX(XFD:XFD,1):INDEX(XFD:XFD,3)

There 3 is n.

Used in Formula

=INDEX("Constant"&INDEX(XFD:XFD,1):INDEX(XFD:XFD,3),0)

Evaluate Formula shows that it works:

Here column XFD is used because in most cases this column will be empty and a column which is guaranteed to be empty is needed for this solution.

If used

"Constant"&T(ROW($A$1:INDEX($A:$A,3)))

=INDEX("Constant"&T(ROW($A$1:INDEX($A:$A,3))),0)

the need of an empty column disapears. The function ROW returns numbers but the T returns an empty string if it's parameter is not text. So empty strings will be concatenated for each 1:3 (n).

Thanks to @MacroMarc for the hint.




回答2:


EDIT Reading @AxelRichter answer, I see I should also indicate that the formulas below assume Constant is a number. If Constant is text, then this solution will not work.

Volatile:

=ROW(INDIRECT("1:" & Repts))/ROW(INDIRECT("1" & ":" & Repts)) * Constant

non-Volatile:

=ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,Repts,1))/ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,Repts,1))*Constant

If

Constant = 14
Repts = 3

then

Result = {14;14;14}

The first part of the formulas create an array of 1's repeated Repts times. Then we multiply that array by Constant to get the desired result.

And after reading @MacroMarc's comment, the following non-volatile formula shouyld also work for numbers:

=(ROW($A$1:INDEX($A:$A,Repts))>0)*Constant


来源:https://stackoverflow.com/questions/50526178/excel-creating-an-array-with-n-times-a-constant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!