How To Select A Column Based On The Column Header In Excel?

Supposing, you have a large worksheet which contains multiple columns, and now, you would like to select the specific column based on a column header name. To find the column by column in a large worksheet will waste much time, this article, I will introduce a quick method to solve this job in Excel.

The following VBA code can help you to select the columns based on a specific header name, please do as this:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Select column based on column header name:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Sub FindAddressColumn()
'Updateby Extendoffcie
    Dim xRg As Range
    Dim xRgUni As Range
    Dim xFirstAddress As String
    Dim xStr As String
    On Error Resume Next
    xStr = "Name"
    Set xRg = Range("A1:P1").Find(xStr, , xlValues, xlWhole, , , True)
    If Not xRg Is Nothing Then
        xFirstAddress = xRg.Address
        Do
            Set xRg = Range("A1:P1").FindNext(xRg)
            If xRgUni Is Nothing Then
                Set xRgUni = xRg
            Else
                Set xRgUni = Application.Union(xRgUni, xRg)
            End If
        Loop While (Not xRg Is NothingAnd (xRg.Address <> xFirstAddress)
    End If
    xRgUni.EntireColumn.Select
End Sub

Note: In the above code, A1:P1 is the range of headers that you want to select columns from, and “Name” in the script xStr = "Name" is the header name that you want to select columns based on. Please change them to your need.

3. After copying and pasting the code, please press F5 key to run this code, and all the columns with the specific header name have been selected at once, see screenshot:

Read 57 times
KT_Austin

About:

Welcome to AustinLaptop.Com

Subscribe to our Newsletter
Top
We use cookies to improve our website. By continuing to use this website, you are giving consent to cookies being used. More details…