So, you’re working with Google Sheets and you realize, “Hey, I need to get a specific column. But wait… I need to be able to identify that column by name (i.e. the value of the cell in the first row) because I can’t be sure of the letter that represents each column.” You can do this. Here’s how.
Now, at first you might think you’d need to create a script to retrieve the first row to select a column in Google Sheets by referencing a cell value in the first row, but you can actually do this quite easily with a relatively simple formula.
Our goal for this formula is to get the letter name (A, B, C, etc.) of the column where a cell’s value in the first row matches the value we’re looking for.
But before we get started, let’s set up an example. Here’s a table of data.
The first step is to search the first row for the desired column name and return the column’s position. To do this, we’ll use MATCH.
The will return the value “3“. In other words, the formula has found the value “Year” in the third column of the first row.
So, now we know the column number. But now we need to transform that number into the corresponding letter.
To accomplish this, we’ll use the ADDRESS function to retrieve the cell’s coordinates. The address function requires two parameters: 1) the column number, and 2) the row number. We now know the column number. And, of course, we’ve known the row number all along: 1. (Our column names are in the first row.)
So, our address function looks like this:
You may have noticed the third parameter in that formula. That third parameter tells Google Sheets whether the references to the row and column are relative or absolute. I’ve set the value to 4 which tells Google Sheets that both references are relative.
The ADDRESS function returns the cell reference as a string. In this case, we get “C1“.
Hey! There’s that column letter that we’re looking for. If only there were a function to get rid of that pesky row ID… 😉
As you’ve probably guessed, we’ll apply SUBSTITUTE to get rid of the “1”, leaving just “C“.
And there you have it. You’re now left with the letter name of the column where the cell in the first row matches the value you’re looking for. In other words, you’ve just looked up a column by its name (or “label”, if you prefer).
Bonus How To
Suppose you wanted to retrieve the entire column. In other words, suppose you wanted to get all the rows from the column you’ve just looked up. There are a couple ways you could do this.
Use INDIRECT to get a column by name / label
The INDIRECT function returns a cell reference specified by a string. What this means is that you can tell the INDIRECT function, “Hey, get me the column of data whose reference is represented by ‘data!C:C'”.
The first step is to create the “string” that represents the column you want to retrieve. Of course, we want to get the column by name. So, we’ll incorporate our fancy column identification formula.
That gives us “C”, right? We want our final string result to look like “data!C:C”. Here’s how to accomplish that.
Now, just pass that formula to your indirect function.
Use QUERY to get specific rows by column name / label
Suppose that, rather than retrieving the values from a specific column, you wanted to retrieve a specific set of rows where the value in a specific column matches a value of your choosing. For example, suppose you wanted to say something like “Get me all the rows where the value of the column named ‘Year’ is ‘2010’.”
This is where the QUERY function really comes in handy.
=QUERY(data!A:C,"SELECT * WHERE "&SUBSTITUTE(ADDRESS(1,MATCH("Year",data!A1:C1,0),4),1,"")&"=2010")
Now you have at your disposal some powerful functions for looking up data by column name. I hope this proves useful. Please share your comments, ideas, and suggestions below.