Using indirect to pull from multiple sheets for a Query

拈花ヽ惹草 提交于 2019-12-11 17:05:39

问题


I have a dynamic list of the sheets in a Google workbook. I am hoping to create a query that pulls from these different sheets - although I am often adding and deleting sheets.

Right now my query looks like, and it works great:

=query({'1.17 invoice'!A25:N;'1.31 invoice'!A25:N;'2.1 invoice'!A25:N;'invoice 2.1 (2)'!A25:N},"SELECT Col1, Col2, Col3, Col7, Col12, Col13 where Col1 <> ''",0)

I can create a cell A1 that has a value of: '1.17 invoice'!A25:N;'1.31 invoice'!A25:N;'2.1 invoice'!A25:N;'invoice 2.1 (2)'!A25:N

But right now I have to copy and paste the results of A1 into the query formula in order for it to update. When I try to create a formula like:

=query({indirect(A1)},"SELECT Col1, Col2, Col3, Col7, Col12, Col13 where Col1 <> ''",0)

I just get an error that 'the results of the indirect are not a valid cell range'. Is there any good way to automate getting the list of sheets into the query function?


回答1:


The OP has tried to make the array range more flexible. Original formula:

=query({'1.17 invoice'!A25:N;
        '1.31 invoice'!A25:N;
         '2.1 invoice'!A25:N;
     'invoice 2.1 (2)'!A25:N},
 "SELECT Col1, Col2, Col3, Col7, Col12, Col13 where Col1 <> ''",0)

The OP entered the array range in cell A1, and modified the formula:

A1 = '1.17 invoice'!A25:N;'1.31 invoice'!A25:N;'2.1 invoice'!A25:N;'invoice 2.1 (2)'!A25:N

Modified formula:

=query({indirect(A1)},"SELECT Col1, Col2, Col3, Col7, Col12, Col13 where Col1 <> ''",0)

This doesn't work because INDIRECT returns a single array value, but A1 consists of several ranges. However, if each of the ranges is declared separately, the query will work.


Layout:

A1 = '1.17 invoice'!A25:N    
A2 = '1.31 invoice'!A25:N    
A3 = '2.1 invoice'!A25:N    
A4 = 'invoice 2.1 (2)'!A25:N 

Formula:

=query({indirect(A1);
        Indirect(A2);
        indirect(A3);
        Indirect(A4)},
 "SELECT Col1, Col2, Col3, Col7, Col12, Col13 where Col1 <> ''",0)

Credit:
Ruben (https://webapps.stackexchange.com/a/88726/196152)
Karl_S (https://webapps.stackexchange.com/a/104517/196152)



来源:https://stackoverflow.com/questions/54495154/using-indirect-to-pull-from-multiple-sheets-for-a-query

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