Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Finding Columns of a Certain Width.

Finding Columns of a Certain Width

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


Howard has a need to discover all the columns in a worksheet that are a given width. For instance, he needs to know which columns have a width of 3.6.

This can be done by using a macro. One of the properties your macro can access is the width of each column. This means that you can step through the columns and check those widths against the desired width (3.6) in the following manner:

Sub ListColumns()
    Dim dColWidth As Double
    Dim sMsg As String
    Dim x As Integer

    dColWidth = 3.6
    sMsg = ""
    For x = 1 To ActiveSheet.Columns.Count
        If Columns(x).ColumnWidth = dColWidth Then
            sMsg = sMsg & vbCrLf & x
        End If
    Next
    If sMsg = "" Then
        sMsg = "There are no columns with" & _
          vbCrLf & "a width of " & dColWidth
    Else
        sMsg = "The following columns have" & _
          vbCrLf & "a width of " & dColWidth & _
          ":" & vbCrLf & sMsg
    End If
    MsgBox sMsg
End Sub

This macro displays a message box that lists the columns that match the desired width. The macro can be made more robust with some simple changes. For instance, the following example prompts the user for a column width, counts the number of matches, and even compensates if the worksheet is using R1C1 referencing mode.

Sub Find_ColumnWidth()
    Dim Col As Integer          ' Column (loop variable)
    Dim ColsFound As Integer    ' Columns Found Count
    Dim Desired_Width As Double ' Column Width To Find
    Dim OutStr As String        ' Output String
    Dim Title As String         ' Msgbox Title
    Dim I As Integer
    Dim S As String

    ' Find out column width wanted
    S = InputBox("Enter ColumnWidth to find ?", _
      " Find ColumnWidth on " & ActiveSheet.Name)
    Desired_Width = Val(S)
    If Desired_Width = 0 Then Exit Sub

    ' Initialize Columns Found Count and Output String
    ColsFound = 0
    OutStr = ""

    For Col = 1 To ActiveSheet.Columns.Count
        If Columns(Col).ColumnWidth = Desired_Width Then
            ColsFound = ColsFound + 1

            If Application.ReferenceStyle = 1 Then
                ' Using "A1" format
                S = Cells(1, Col).Address(ReferenceStyle:=xlA1)
                S = Mid(S, 2, Len(S) - 3)
            Else
                ' Using "R1C1" format
                S = Trim(Str(Col))
            End If
            OutStr = OutStr & S & vbCrLf
        End If
    Next

    ' Construct MsgBox Title string
    Title = "Width=" & Desired_Width _
      & " on " & ColsFound & " column" _
      & Left("s", - (ColsFound > 1)) & " "

    If ColsFound = 0 Then
        OutStr = "No matches found"
    End If

    MsgBox OutStr, vbOKOnly, Title
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11654) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Finding Columns of a Certain Width.

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

Using the Status Bar

When developing a macro, you may want to display on the status bar what the macro is doing. Here's how to use this ...

Discover More

Page Numbers in VBA

When you print a larger worksheet, Excel breaks the printout across several pages. You may want to know, before you ...

Discover More

Automatically Updating Styles

When you add formatting to some text in your document, Word may apply your formatting to every other part of your ...

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)

Determining the Number of Visible Columns

When using a macro to process information in a worksheet, you may want that macro to figure out how many columns are ...

Discover More

Creating a Floating Macro Button

Macros can make your use of Excel much more powerful. If you have a macro that is triggered by an on-screen button, you ...

Discover More

Adjusting Values with Formulas

Paste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...

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 6 - 6?

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.