Counting Ports of Call with a PivotTable

Written by Allen Wyatt (last updated May 9, 2026)

1

Dutch has a worksheet he uses to track all of the cruises he has taken. Each row is one cruise. The left-hand columns contain ship name, departure date, booking number, etc. The ten right-most columns are labeled "Port 1" through "Port 10" to record the ports visited, in order. Nassau, for instance, might be the first stop on one cruise and the third stop on another. Dutch can't figure out a simple, straightforward way to use a PivotTable (as he does, e.g., to see how many times he's cruised on a particular ship, etc.) to see how many times he's visited each port. He wonders if a PivotTable can be used to count the number of times each unique value appears in a range of cells as opposed to a single row or column.

Dutch can solve this problem without even resorting to a PivotTable. This can be done by selecting the cells containing the ports and giving that range a name, such as "Ports." (How you set up a named range has been covered in other ExcelTips.)

At this point there are two ways you can go, depending on your version of Excel. If you are using Excel 2024 or Microsoft 365, you can use the following to provide a list of all the port names:

=SORT(UNIQUE(TOCOL(Ports,1)))

If you are using an older version of Excel, then the list will need to be manually created. The list could be placed on the same worksheet that Dutch is using, or it could be on a different worksheet in the same workbook.

Let's say, for instance, that the port names are in column A, starting below the detailed data in row 22. To the right of the port names, in column B, you could add a simple COUNTIFS formula that references the named range you created:

=COUNTIFS(Ports, A22)

Copy this down as many cells as necessary, and you will have a count of how many times each port was visited. (See Figure 1.)

Figure 1. Getting a Count of Ports

The downside to this approach, of course, is that you'll need to make sure that the Ports named range refers to the proper range whenever you add new cruises. You'll also need to manually add any ports desired to the port list if you are using Excel 2019 or older.

Of course, you can bypass putting a formula in column B completely, provided you are using Excel 2024 or Microsoft 365. It just takes a slightly different formula in cell A22:

=LET(x,TOCOL(Ports,1),y,SORT(UNIQUE(x)),HSTACK(y,COUNTIFS(Ports,y)))

This single formula provides the port list and the count for each port.

If you absolutely want to use a PivotTable, then you will need to restructure your data. PivotTables are used to analyze the contents of rows and columns, as implied in Dutch's original question about whether a range could be analyzed. So, instead of having 10 columns to track each port of call, you could have multiple rows that define each cruise, with each row being a port of call. (See Figure 2.)

Figure 2. Reorganizing Your Data

With the data in a format such as this, you can easily create a PivotTable based on the contents of the Port column that would show the count for each port visited. Restructuring in this way—with each row being a port visited—makes the data much more conducive to analyzing with a PivotTable.

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

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

Changing Color in WordArt

You can make your document flashier by using WordArt to punch it up. Here's how you can change the color of the text in ...

Discover More

Searching for Text With a Certain Format

The Find and Replace tool in Word is very powerful. You can use it to search not only for text but for the formatting ...

Discover More

Storing Macros in Templates

How Excel uses templates is different than how Word uses templates. This tip looks at those differences and discusses ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2019 For Dummies today!

More ExcelTips (ribbon)

Seeing What Changed in a PivotTable

PivotTables are great for aggregating and analyzing tons of raw data. If you want to see what changes in the PivotTable ...

Discover More

Empty PivotTable Cells Don't Show as Blank

You can configure Excel so that it displays special text within blank PivotTable cells. Here's how to make the changes ...

Discover More

Easy Filtering Specifications for a PivotTable

When you want to include specific records from a source table into a PivotTable, you need to employ some sort of ...

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 four more than 7?

2026-05-13 15:19:13

J. Woolley

The Tip uses this formula in cell B22n=COUNTIFS(Ports, A22)nbut this formula might be more appropriaten=COUNTIF(Ports, A22)nAnd if the list of unique ports starting in A22 is the result of this formulan=SORT(UNIQUE(TOCOL(Ports, 1)))nthen this formula can be used in cell B22 n=COUNTIF(Ports, A22#)nFinally, the Tip suggests this formula in cell A22 returning a two column arrayn=LET(x,TOCOL(Ports,1),y,SORT(UNIQUE(x)),HSTACK(y,COUNTIFS(Ports,y)))nbut here's an alternative with the same result (sorted by Port name)n=LET(a, SORT(UNIQUE(TOCOL(Ports, 1))), HSTACK(a, COUNTIF(Ports, a)))nOr you could sort by number of visits (descending)n=LET(a, UNIQUE(TOCOL(Ports, 1)), SORT(HSTACK(a, COUNTIF(Ports, a)), 2, -1))


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.