Consulting

Results 1 to 5 of 5

Thread: Select a column by matching the column heading content?

  1. #1
    VBAX Newbie
    Joined
    Jan 2008
    Posts
    3
    Location

    Select a column by matching the column heading content?

    Hi,
    I want to find the location and select the column by matching the content of the column heading. For example, I want to select the column in which the first cell (column heading) contains string "address", and thus locate and select the whole column.


    Thanks very much.

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This is one way.
    Sub test()
    Dim foundCell As Range
    With ThisWorkbook.Sheets("sheet1")
        On Error Resume Next
        Set foundCell = .Range("1:1").Find(What:="address", After:=.Range("a1"), LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False)
        On Error Resume Next
    End With
    If foundCell Is Nothing Then
        MsgBox "No ""address"""
    Else
        MsgBox "Found in column " & foundCell.Column
    End If
    End Sub

  3. #3
    VBAX Newbie
    Joined
    Jan 2008
    Posts
    3
    Location
    Thank you.
    I am quite new to VBA. could you please tell me how should I add these code? May I add directly to a command button?
    shall I change "sheet1" to sheetname, where sheetname is a self-declared string variable?
    also, after knowing the location of the column, i would like to show the contents of each cell in the column, row by row. so instead of displaying a messagebox, i would like the program to note down the location for later implementations.

    thanks once more (:

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    From the keyboard, I would use Find from the edit menu, rather than this routine.
    If you want a toolbar button, Customize toolbars will supply one.

  5. #5
    VBAX Newbie
    Joined
    Jan 2008
    Posts
    3
    Location
    I am not using the keyboard, since everytime the workbook would be different, so is the location of the column "address". So I want to locate the column of "address" every time by a code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •