Determining the Number of Visible Columns

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


Dave needs a way, in a macro, to determine how many columns are visible in the current window or pane.

This is actually quite easy to accomplish. The trick is to remember that you need to work with the VisibleRange object, which belongs to the ActiveSheet object. You can use the .Columns collection for the VisibleRange object, and then utilize the .Count method for that collection. What you end up with is the number of columns, in this manner:

Sub VisibleColCount()
    Dim sTemp As String

    sTemp = "There are "
    sTemp = sTemp & ActiveWindow.VisibleRange.Columns.Count
    sTemp = sTemp & " columns visible."
    MsgBox sTemp
End Sub

Seems simple, right? The problem is that the .Count method returns the number of columns in the collection whether they are hidden or not. For instance, let's say that you look at the screen and you see that it shows columns C through H. That means that 6 columns are displayed, and that is what the .Count method returns in the above macro. If you then hide column F, now columns C through I are displayed. Instead of .Count still returning 6 (representing columns C, D, E, G, H, and I), it now returns 7, which includes the hidden column, even though it is not visible.

The solution to this situation is to check the .Hidden property of each column in the .Columns collection. Only if the .Hidden property is False should the column be counted as visible, in this manner:

Sub VisibleColCount()
    Dim c As Range
    Dim iCount As Integer
    Dim sTemp As String

    iCount = 0
    For Each c In ActiveWindow.VisibleRange.Columns
        If Not c.Hidden Then iCount = iCount + 1
    Next c

    sTemp = "The active window includes "
    sTemp = sTemp & ActiveWindow.VisibleRange.Columns.Count
    sTemp = sTemp & " columns. Of those columns, "
    If iCount = ActiveWindow.VisibleRange.Columns.Count Then
        sTemp = sTemp & "all "
    Else
        sTemp = sTemp & "only "
    End If
    sTemp = sTemp & iCount & " are visible."
    MsgBox sTemp
End Sub

There is one other gotcha with this approach: As far as VBA is concerned, a column is counted as visible as long as just a sliver of that column is showing. Also, if you anticipate using panes in your Excel display, you may want to consider adding the ActivePane object into the mix. Basically, it takes a simple change in the above macros: Anyplace you see the ActiveWindow object, follow it with ActivePane, similar to this:

    For Each c In ActiveWindow.ActivePane.VisibleRange.Columns

This ensures that your macro is looking at only the current pane in the window when doing its counting.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (617) 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

Making Bookmarks Bold

Do you want an easy way to see all the bookmarks in your document? Word provides a way to make them visible, or you can ...

Discover More

Smart Quotes in AutoCorrect Entries

Smart quotes can add a finishing touch to your text. You might expect that when AutoCorrect is used to add text, it would ...

Discover More

Spell-Checking from the Keyboard

If you hate to take your hands from the keyboard, even to right-click on a word, you'll love the information in this tip. ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Disabling Shift Key Use when Opening a Workbook

Open up a workbook, and Excel normally runs the macros associated with that workbook. You can disable the automatic ...

Discover More

Copying Worksheets in a Macro

Copying worksheets (one or many) is easy to do manually. What is not well known is that it is even easy to make the ...

Discover More

Determining a Format's Currency Symbol

Excel allows you to format numeric values in many different ways, including as currency. If you want to determine, in a ...

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 seven more than 1?

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.