Excel - Identify if Worksheet exists between two worksheets?

Viewed 38

I have 5 worksheets: (1) Summary (2) 2020 (3) 2019 (4) 2018 (5) 2017

In my "Summary" worksheet, I have Column A with cells each containing the name of each worksheet (A1: 2020, A2: 2019, A3: 2018, A4: 2017) and Column B with the number "1" in each corresponding row.

Let's say I delete the "2019" worksheet so that it is no longer between worksheets "2020" and "2017" - is there any formula that I could use to flag that in row 2? Perhaps through some sort of "if(..,1,0)" formula for Column B? I do not want to use VBA.

1 Answers

There are various error-checking functions available in Excel, and most of them would be appropriate for your situation. The simplest would be to use IFERROR and just refer to a cell on one of the sheets, like this:

=IFERROR('2020'!A1,"Missing")

If the formula receives an error value because it can't find the cell, it will display the text "Missing"; otherwise, it will show the value of the cell.

The disadvantage of doing it this way, hard-coding the sheet name into the function, is that if the sheet is deleted the sheet name will be replaced in the formula with #Ref!, so even if the sheet is restored the formula will need to be changed.

We can enhance the functionality by using an INDIRECT along with the IFERROR. This will take a text string and turn it into a cell reference. So assuming we have worksheet names in column A, we could do this:

=IFERROR(Indirect(A1 & "!A1"),"Missing")

The INDIRECT function takes the value in A1 and combines it with the text string, and reads the whole thing as a cell reference. This has the same result, but is more robust since deleting the sheet and then replacing it will clear the error.

For a slightly more elaborate result, we could next IF and ISERROR instead of using IFERROR. It gives us more control over the result.

=IF(ISERROR(INDIRECT(A1 & "!A1")),"Missing","Found")

ISERROR returns TRUE if the first argument throws an error, an FALSE if it does not. This will return "Found" if the sheet is present, or "Missing" if it isn't.

Related