Finding a Worksheet with a Specific Value in a Specific Cell

Written by Allen Wyatt (last updated November 5, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


Kelly has a workbook that contains hundreds of worksheets. Each worksheet represents an individual project for her company. Cell C4 of each worksheet contains the project number for that particular project. Finding individual projects out of all the hundreds in the workbook is cumbersome, at best. She wonders if macro could be created that would allow the user to enter a job number, find the worksheet that has that job number in cell C4, and then display that worksheet.

Before addressing how to do this in a macro, you should understand that you may be able to find the desired project using Excel's native tools. All you need to do is to follow these steps:

  1. Press Ctrl+F. Excel display the Find tab of the Find and Replace dialog box.
  2. Click the Options button to expand the dialog box. (See Figure 1.)
  3. Figure 1. The expanded Find tab of the Find and Replace dialog box.

  4. Using the Within drop-down list, choose Workbook.
  5. If cell C4 in each worksheet contains only the project number, select the Match Entire Cell Contents check box.
  6. Click Find Next.

That's it; you should be taken to the first instance of cell that contains your project number. If your workbook contains project numbers in multiple cells (besides cell C4), then using the Find tool can be a bit tedious; you'll need to keep clicking Find Next until you locate the correct worksheet. In such a situation, a macro may be the best solution because it allows you to specifically look at cell C4 and ignore whatever is in any other cell.

The following macro prompts the user to enter a project number. It then searches each worksheet in the workbook for the first worksheet in which cell C4 contains that project number. If found, the macro displays the worksheet and activates the cell. If not found, a message is displayed indicating such.

Sub FindProject1()
    Dim wks As Worksheet
    Dim sCell As String
    Dim sProj As String

    sCell = "C4" 'cell with Project number
    sProj = InputBox("What Project are you looking for?")
    For Each wks In Worksheets
        If wks.Range(sCell) = sProj Then
            wks.Activate
            wks.Range(sCell).Activate
            MsgBox ("Project '" & sProj & "' is in:" & vbCrLf & wks.Name)
            Exit Sub
        End If
    Next wks
    MsgBox ("Project is not found")
End Sub

There are two potential problems with this macro. First, if your project number is actually a number, the macro may not find it as it looks for text strings. Second, if there are multiple worksheets that use the same project number, you'll never know it because the macro always stops at the worksheet with the first matching project number in cell C4. You can overcome both of these problems by making a couple of changes in the macro.

Sub FindProject2()
    Dim wks As Worksheet
    Dim sCell As String
    Dim sProj As String
    Dim vSheets As New Collection
    Dim sTemp As String

    sCell = "C4" 'cell with Project number
    sProj = InputBox("What Project are you looking for?")
    For Each wks In Worksheets
        If wks.Range(sCell).Value = sProj Or _
          wks.Range(sCell).Value = Val(sProj) Or _
          wks.Range(sCell).Text = sProj Then
            vSheets.Add wks
        End If
    Next wks

    Select Case vSheets.Count
        Case 0
            sTemp = "Project " & sProj & " was not found "
            sTemp = sTemp & "in this workbook."
            MsgBox sTemp
        Case 1
            wks.Activate
            wks.Range(sCell).Activate
        Case Else
            sTemp = "Project " & sProj & " was found on more "
            sTemp = sTemp & "than one sheet:" & vbCrLf
            For Each wks In vSheets
                sTemp = sTemp & wks.Name & vbCrLf
            Next wks
            MsgBox sTemp
    End Select
End Sub

With this version of the macro, the project worksheet is displayed only if the project number is unique among all the worksheets. If there are multiple instances where the project number is the same, you are notified of the name of those worksheets, but no worksheet is selected.

You could, if desired, make the macro much simpler by renaming all your worksheets to the project number. This would also have the added benefit of ensuring that no project actually uses two worksheets. (All worksheet names must be unique, so if each worksheet uses a project number as a name, you could only have one worksheet per project number.) If you take this approach, you could use a macro similar to the following to find and jump to the desired worksheet:

Sub FindProject3()
    On Error GoTo ErrorHandler
    Sheets(InputBox("Enter Project Number:")).Activate
    Exit Sub
ErrorHandler: MsgBox "No such Project"
End Sub

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13489) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Deleting Commented Text

Word allows you to add comments to your document to aid in the development of the document. At some point you may want to ...

Discover More

Printing a Portion of a Worksheet

Need to print a portion of a worksheet, but don't want to waste paper by printing the whole thing? It's easy to print ...

Discover More

Microsoft Word's Amazing Autos

Word provides several tools that can aid in developing your documents. This e-book focuses on a few of those tools. You ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Selecting All Visible Worksheets in a Macro

Do you need your macro to select all the visible worksheets (and just the visible ones)? It's not as easy as it sounds, ...

Discover More

Sorting Worksheets According to Region

Sorting worksheet tabs can be done by using a macro. This tip provides a macro that accomplishes this task, but it also ...

Discover More

Returning a Worksheet Name

Need to know the name of the current worksheet? You can use the CELL function as the basis for finding this information ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.