Results 1 to 20 of 62

Thread: VBA Code to Match US Region to the State that is entered

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #25
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    1. I added new sub LUV to return the n-th row bu searching the col headers row to find the column string each time


    I added error message to help figure out what was wrong with the column header

    I would have been a LOT easier if the column headers in MDS and MOST were called the same


    
    Option Explicit
    
    'Look Up value
    Function LUV(rColHeaders As Range, sColHeader As String, N As Long) As Range
        Dim i As Long
        
        i = 0
        
        On Error Resume Next
        i = Application.WorksheetFunction.Match(sColHeader, rColHeaders, 0)
        On Error GoTo 0
            
        If i <> 0 Then
            Set LUV = rColHeaders.Parent.Cells(N, i)
        Else
            MsgBox "Column " & sColHeader & " not found in row " & rColHeaders.Address & " on worksheet " & rColHeaders.Parent.Name
        End If
    End Function
    Function GetColumnNumber(sColHeader As String, rColHeaders As Range) As Long
        Dim i As Long
        i = 0
        
        On Error Resume Next
        i = Application.WorksheetFunction.Match(sColHeader, rColHeaders, 0)
        On Error GoTo 0
            
        If i <> 0 Then
            GetColumnNumber = i
        Else
            MsgBox "Column " & sColHeader & " not found in row " & rColHeaders.Address & " on worksheet " & rColHeaders.Parent.Name
        End If
    End Function

    Additional Question: Why do I get the following when I go to look at my macro's
    Probably because you had 2 workbooks open with the same sheet names and macro names
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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