+ Reply to Thread
Results 1 to 3 of 3

Script to find column header doesn't work if it is second to last or last column.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Script to find column header doesn't work if it is second to last or last column.

    Sub columnfind()
    Cells(1, 1).Select
    Do Until ActiveCell.Value = "My Column Header"
    
       If ActiveCell.Column > ActiveSheet.UsedRange.Columns.Count Then Exit Do
       ActiveCell.Offset(, 1).Select
    Loop
    
    End Sub
    This works, UNLESS "My Column Header" is the last column, or second to last column, then it jumps left two columns, instead of landing on the correct column.

    The purpose of this script is to select a cell directly in that column that I was searching for.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,101

    Re: Script to find column header doesn't work if it is second to last or last column.

    Try:
    Sub FindHeader()
        Dim header As String
        header = "My Column Header"
        Dim foundHeader As Range
        Set foundHeader = ActiveSheet.Rows(1).Find(header, LookIn:=xlValues, LookAt:=xlWhole)
        If Not foundHeader Is Nothing Then
            Cells(1, foundHeader.Column + 1).Select
        End If
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Script to find column header doesn't work if it is second to last or last column.

    try:
    Sub MyColumn()
    With ActiveSheet
        .Cells(2, .Range("1:1").Find("My Column Header").Column).Select
    End With
    End Sub
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] My code to find longest string in column doesn't work properly! Need Help!!
    By refree in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-03-2013, 08:27 AM
  2. Replies: 2
    Last Post: 04-26-2013, 01:16 PM
  3. Search for column header and find all values and their row numbers in that column
    By woody83 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-14-2012, 03:11 PM
  4. Replies: 8
    Last Post: 05-10-2012, 09:19 AM
  5. return column header of first column that doesn't contain 0
    By jasonanthony in forum Excel General
    Replies: 3
    Last Post: 02-09-2009, 05:36 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1