简体   繁体   中英

Comparing arrays using IF - How to get rid of empty spaces in final result?

I have 2 columns in Sheet 1 (named Projects), "Projects" in column A and "Status" in column B. The number of rows is dynamic.

In Sheet 2, I want to extract the projects that meet a certain string criteria and populate a table. For example, if the Status is "Operating", take the project name in column A for that row and put it in a table.

I tried =IF(Projects!B1:B="Operating",Projects!A1:A,"") but it returns the names of the projects in the same row position as in Sheet 1. So if there is a 2 row gap in Sheet 1 between two operating projects, it keeps the 2 row gap in Sheet 2.

How do I get rid of the gaps?

There are some ways to accomplish this.

First solution - based on code you have provided

Your code: =IF(Projects!B1:B="Operating",Projects!A1:A,"")

After some tweaks (working):

=FILTER(
  ArrayFormula(IF(Projects!B1:B="Operating",Projects!A1:A,"")),
  ArrayFormula(IF(Projects!B1:B="Operating",Projects!A1:A,""))<>""
)

As a bonus - you need this code only in one cell

Picture:

在此处输入图片说明

Second solution (elegant and fast one)

You need only this:

=QUERY(Projects!A1:B,"select A where B='Operating'")

Picture在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM