Google sheets query indirect question

Ronderbecke

Board Regular
Joined
Oct 4, 2017
Messages
73
I am trying to figure out why the formula I am using doesn't work. Here is what works:

=Query(Calculator!B2:BP,"select * order by B desc")
But I need the "B" to be variable based on a cell so I am using this function which should be exactly the same but doesn't work:
=Query(Calculator!B2:BP,"select * order by '"&indirect("A2")&"' desc")

Any help would be greatly appreciated. Been spending forever trying to figure out why it's not working.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
can you provide a few rows of your Calculator Sheet (I assume Google Sheets)?

Try this. If it does not work please send some rows from your Sheet.

=Query(Calculator!B2:BP,"select * order by '" & Range("A2") & "' desc") - the value in Range("A2") needs to be the Column (e.g. "D") you are sorting on.
 
Last edited:
Upvote 0
It's weird cause it just says range is an unknown function? Here is what the columns look like

Customer NameSales RepresentativeDate of SalePrice Sheet
Anthony HunterCaryn Bladzik6/3/20225/14/2022
James Nancy O'banionHennessey Costigan9/19/20229/6/2022
Angie WilsonHennessey Costigan10/4/20219/17/2021
Tad MarshburnCaryn Bladzik7/1/20225/14/2022
 
Upvote 0
can you provide a few rows of your Calculator Sheet (I assume Google Sheets)?

Try this. If it does not work please send some rows from your Sheet.

=Query(Calculator!B2:BP,"select * order by “ & Range("A2") & " desc") - the value in Range("A2") needs to be the Column (e.g. "D") you are sorting on.
Here is what works

=Query(Calculator!B2:BP,"select * order by '" & A2 & "' desc")

Where cell A2 contains the column you want to sort on, e.g A, B, C …

If your query range includes header row(s) specify the number of header rows as the 3rd arg to the Query function.
 
Upvote 0
=Query(Calculator!B2:BP,"select * order by '" & A2 & "' desc") isnt working either?
Here is what I put in A2:
1691588878106.png
 
Upvote 0
Your Query results cannot be in the same range as your source data. Try putting your sorted data on a different sheet or into different columns on that same sheet.
But, the Query will not work if it attempts to overwrite the data that it is Querying.

Example below with some of your data.
CCustomer NameSales RepresentativeDate of SalePrice Sheet
Anthony HunterCaryn Bladzik6/3/20225/14/2022
James Nancy O'banionHennessey Costigan9/19/20229/6/2022
Angie WilsonHennessey Costigan10/4/20219/17/2021
Tad MarshburnCaryn Bladzik7/1/20225/14/2022
Sorted Outputformula in Cell below: =Query(B1:E5,"select * order by " & A1 & " asc, D", 1)
Customer NameSales RepresentativeDate of SalePrice Sheet
Anthony HunterCaryn Bladzik6/3/20225/14/2022
Tad MarshburnCaryn Bladzik7/1/20225/14/2022
Angie WilsonHennessey Costigan10/4/20219/17/2021
James Nancy O'banionHennessey Costigan9/19/20229/6/2022
 
Upvote 0
Your Query results cannot be in the same range as your source data. Try putting your sorted data on a different sheet or into different columns on that same sheet.
But, the Query will not work if it attempts to overwrite the data that it is Querying.

Example below with some of your data.
CCustomer NameSales RepresentativeDate of SalePrice Sheet
Anthony HunterCaryn Bladzik6/3/20225/14/2022
James Nancy O'banionHennessey Costigan9/19/20229/6/2022
Angie WilsonHennessey Costigan10/4/20219/17/2021
Tad MarshburnCaryn Bladzik7/1/20225/14/2022
Sorted Outputformula in Cell below: =Query(B1:E5,"select * order by " & A1 & " asc, D", 1)
Customer NameSales RepresentativeDate of SalePrice Sheet
Anthony HunterCaryn Bladzik6/3/20225/14/2022
Tad MarshburnCaryn Bladzik7/1/20225/14/2022
Angie WilsonHennessey Costigan10/4/20219/17/2021
James Nancy O'banionHennessey Costigan9/19/20229/6/2022
So basically I would Query over to the new sheet from the old one, then run a query on that sheet from what is already pulled over so the value is on the same sheet? That would require me to query to cell BT:EH for the first query, and then cell B:BP would be the new query with your formula? In doing it that way it still doesn't sort the data though?
 
Upvote 0
What I would do is create a sheet to do your Query (including) any sorting you want to do.

Your query will reference your source data by using the sheet name in the range, e.g. =Query(Calculator!B2:E5, ….)

As far as sorting is concerned can you provide the formula? I am not having any problems with SQL in the query formula.)
 
Upvote 0

Forum statistics

Threads
1,215,079
Messages
6,122,998
Members
449,092
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top