I'm using the query function in google sheets... I want to select columns F to AB.
I need a cell that has the output F,G,H,I... ,AB
so I can put it in the Select
statement.
This formula should have 2 inputs, the starting letter (F
in this case) and number of following columns (22
in this case)
No app script code please. I know someone can do it with just formulas in one cell.
I believe your goal as follows.
F,G,H,I... ,AB
by giving the start column letter of "F" and the number of columns of 22
.
22
are given, you want to retrieve the value of F,G,H,I... ,AB
and you want to use this like =QUERY(A1:AB,"SELECT F,G,H,I... ,AB")
.For this, how about this answer?
In this sample formula, in order to create the value of F,G,H,I... ,AB
by giving the start column letter of "F" and the number of columns of 22
, I would like to propose the following formula.
=TEXTJOIN(",",TRUE,ARRAYFORMULA(REGEXREPLACE(ADDRESS(1,COLUMN(INDIRECT(A1&"1:"&ADDRESS(1,B1+COLUMN(INDIRECT(A1&"1")),4))),4),"\d+","")))
A1
and B1
have the values of F
and 22
, respectively, the flow of this formula is as follows.
COLUMN
, retrieve the column numbers.ADDRESS
, retrieve the column letters from the column numbers. At that time, the row number os removed using REGEXREPLACE
.ARRAYFORMULA
.TEXTJOIN
, join the column letters with ,
.In this sample formula, =QUERY(A1:AB,"SELECT F,G,H,I... ,AB")
is created using above sample formula 1.
=QUERY(A1:AB,"SELECT "&TEXTJOIN(",",TRUE,ARRAYFORMULA(REGEXREPLACE(ADDRESS(1, COLUMN(INDIRECT(A1&"1:"&ADDRESS(1, B1 + COLUMN(INDIRECT(A1&"1")), 4))), 4),"\d+",""))))
Result:
In this sample result, the range of A2:AB7
is used for QUERY
. So please be careful this. When the cells "A1" and "B1" are changed, the result of this formula is also changed.
When you want to create Col6, Col7, Col8 ... Col28
by giving the start column letter of "F" and the number of columns of 22
, how about the following sample formula?
="Col"&TEXTJOIN(",Col",TRUE,ARRAYFORMULA(COLUMN(INDIRECT(A1&"1:"&ADDRESS(1,B1+COLUMN(INDIRECT(A1&"1")),4)))))
F
and 22
to the cells "A1" and "B1", respectively.