Consulting

Results 1 to 7 of 7

Thread: Concatenating two ranges in MATCH function

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

    Concatenating two ranges in MATCH function

    Greetings to all! This is my first post!

    I've been trying to handle MATCH function, concatenating two ranges for multiple columns search and it keeps telling me Error "13".

    Here is the code line where it stops:

    UserForm1.TextBox2.Text = Application.Index(Sheets("Plan1").Range("A1:E73"), Application.Match(UserForm1.ComboBox1.Text & Month(Date), Sheets("Plan1").Range("A:A") & Sheets("Plan1").Range("B:B")), 4)
    Note that this comand works as a cell formula in the "Match Example" workbook when i use ctrl+shift+enter, but VBA is unable to handle that using the same structure.

    Can someone help?
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    Workaround, and I'd change the logic a little bit


    Option Explicit
    
    
    Dim aryTemp() As String
    Dim rData As Range
    
    Private Sub ComboBox1_Change()
        Dim m As Long
        
        With Me
            If Len(.ComboBox1.Text) = 0 Or Len(.TextBox1.Text) = 0 Then Exit Sub
            
            m = Application.WorksheetFunction.Match(.ComboBox1.Text & "#" & .TextBox1.Text, aryTemp, 0)
        
            Application.EnableEvents = False
            .TextBox2.Text = rData.Cells(m, 4)
            Application.EnableEvents = True
        End With
    End Sub
    
    
    Private Sub CommandButton1_Click()
        Unload Me
    End Sub
    
    
    Private Sub TextBox1_Change()
        Dim m As Long
        
        With Me
            
            If Len(.ComboBox1.Text) = 0 Or Len(.TextBox1.Text) = 0 Then Exit Sub
        
            m = Application.WorksheetFunction.Match(.ComboBox1.Text & "#" & .TextBox1.Text, aryTemp, 0)
        
            Application.EnableEvents = False
            .TextBox2.Text = rData.Cells(m, 4)
            Application.EnableEvents = True
        End With
    End Sub
    
    Private Sub UserForm_Initialize()
        Dim n As Long
        
        Set rData = Sheets("Plan1").Cells(1, 1).CurrentRegion
        
        ReDim aryTemp(1 To rData.Rows.Count)
        
        For n = LBound(aryTemp) To UBound(aryTemp)
            aryTemp(n) = rData.Cells(n, 1) & "#" & rData.Cells(n, 2)
        Next n
        Application.EnableEvents = False
        UserForm1.TextBox1.Text = Month(Date)
        Application.EnableEvents = True
    
    End Sub
    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

  3. #3
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Private Sub ComboBox1_Change()
        Dim f As String
        
        TextBox1.Text = Month(Date)
        f = "INDEX(Plan1!A1:E73,MATCH(""" & ComboBox1.Text & Month(Date) & """,Plan1!A:A&Plan1!B:B,0),4)"
        TextBox2.Text = Evaluate(f)
        
    End Sub

  4. #4
    VBAX Newbie
    Joined
    Jan 2019
    Posts
    3
    Location
    Quote Originally Posted by mana View Post
    Private Sub ComboBox1_Change()
        Dim f As String
        
        TextBox1.Text = Month(Date)
        f = "INDEX(Plan1!A1:E73,MATCH(""" & ComboBox1.Text & Month(Date) & """,Plan1!A:A&Plan1!B:B,0),4)"
        TextBox2.Text = Evaluate(f)
        
    End Sub
    Same error using Evaluate method.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    Did you try the macro in post #2?
    ---------------------------------------------------------------------------------------------------------------------

    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

  6. #6
    VBAX Newbie
    Joined
    Jan 2019
    Posts
    3
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Did you try the macro in post #2?
    Yes, it works. But I was trying to use a simpler logic. In mu original workbook (a lot more complex), I would have to repeat this routine for a large number o fields to fulfill lots of forms. Anyway, your solution works and it made me learn a new logic to use in other ocasions.

    Thanks!

    PS: I decided to create an auxiliar tab to feed the forms. In the worksheet I can use the MATCH method and other people who work with me can understand the code easily if they need to change that when i'm out (It has to be understandable to someone with less knowlege).

    The MATCH method seems to be incompatible to anything but string (including range) when using VBA, it's a pity.

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    Yes, it works. But I was trying to use a simpler logic. In my original workbook (a lot more complex), I would have to repeat this routine for a large number o fields to fulfill lots of forms. Anyway, your solution works and it made me learn a new logic to use in other occasions.

    Thanks!

    PS: I decided to create an auxiliar tab to feed the forms. In the worksheet I can use the MATCH method and other people who work with me can understand the code easily if they need to change that when I'm out (It has to be understandable to someone with less knowledge).
    If the other fields are just additional instances of the 3, you can modularize the logic. Look at the last 2 subs below.

    Making it understandable to someone else is very important, or even yourself 6 months from now when you have to go back and look at it

    You can make this even more general purpose (but more complex) by using Classes. I did a very complex application before I retired where using Class modules and Interface modules would have really helped, but I knew I had to turn it over to someone else so I kept their learning curve as simple as possible



    Capture.JPG



    Option Explicit
    
    Dim aryTemp() As String
    Dim rData As Range
    
    Private Sub ComboBox1_Change()
        Call pvtComboBox_Change(Me.ComboBox1, Me.TextBox1A, Me.TextBox1B)
    End Sub
    
    Private Sub TextBox1A_Change()
        Call pvtComboBox_Change(Me.ComboBox1, Me.TextBox1A, Me.TextBox1B)
    End Sub
    
    Private Sub ComboBox2_Change()
        Call pvtComboBox_Change(Me.ComboBox2, Me.TextBox2A, Me.TextBox2B)
    End Sub
    
    Private Sub TextBox2A_Change()
        Call pvtComboBox_Change(Me.ComboBox2, Me.TextBox2A, Me.TextBox2B)
    End Sub
    
    '===========================================================================================================
    Private Sub UserForm_Initialize()
        Dim n As Long
        
        Set rData = Sheets("Plan1").Cells(1, 1).CurrentRegion
        
        ReDim aryTemp(1 To rData.Rows.Count)
        
        For n = LBound(aryTemp) To UBound(aryTemp)
            aryTemp(n) = rData.Cells(n, 1) & "#" & rData.Cells(n, 2)
        Next n
        Application.EnableEvents = False
        UserForm1.TextBox1A.Text = Month(Date)
        UserForm1.TextBox2A.Text = Month(Date)
        Application.EnableEvents = True
    
    End Sub
    
    
    
    '=========================================================================================================== '===========================================================================================================
    Private Sub CommandButton1_Click() Unload Me End Sub Private Sub pvtComboBox_Change(CB As MSForms.ComboBox, TBA As MSForms.TextBox, TBB As MSForms.TextBox) Dim m As Long If Len(CB.Text) = 0 Or Len(TBA.Text) = 0 Then Exit Sub m = Application.WorksheetFunction.Match(CB.Text & "#" & TBA.Text, aryTemp, 0) Application.EnableEvents = False TBB.Text = rData.Cells(m, 4) Application.EnableEvents = True End Sub Private Sub pvtTextBox_Change(CB As MSForms.ComboBox, TBA As MSForms.TextBox, TBB As MSForms.TextBox) Dim m As Long If Len(CB.Text) = 0 Or Len(TBA.Text) = 0 Then Exit Sub m = Application.WorksheetFunction.Match(CB.Text & "#" & TBA.Text, aryTemp, 0) Application.EnableEvents = False TBB.Text = rData.Cells(m, 4) Application.EnableEvents = True End Sub
    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
  •