+ Reply to Thread
Results 1 to 18 of 18

Edit VBA Userform code to run if only 1 entry that matches criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Question Edit VBA Userform code to run if only 1 entry that matches criteria

    Hi there

    I have the following code:

    Private Sub ComboBox1_Change()
    
    End Sub
    
    Private Sub ListBox1_Click()
    
    End Sub
    
    Private Sub UserForm_Click()
    
    End Sub
    
    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        Selection.Value = ListBox1.Value
        Unload Me
    End Sub
    
    Private Sub UserForm_Initialize()
    Dim rRng As Range, r As Range, rSortCr As String
    
    Set rRng = Sheet2.Range("a2:a5567")
    rSortCr = UCase(ActiveCell.Value)
    
    For Each r In rRng
        If InStr(UCase(r), rSortCr) Then Me.ListBox1.AddItem r.Value
    Next r
    
    End Sub
    Can anyone help me to edit the code so that if there is only one entry that meets the criteria, it will be just put into the selected cell when I run the macro?

    Thank you.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    I am unclear about your requirement Can you please explain which part needs to be edited?


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    What criteria?
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Sorry I'm fairly new to VBA - hence the username..

    At the moment, what it does is searches in ("a2:a5567") and returns any of the entries that match the criteria. The criteria is what I've already typed into the cell. So I type something in and then run the macro and it is effectively a drop down but it comes up in a userform instead.

    This make sense or no?

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    If there's only going to be at most one match, why a combobox? Wouldn't a simple textbox suffice?s

  6. #6
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    No there sometimes will be more than 1 match - normally in fact...

    So I'm basically searching in the range for the text in cell and then returning the results in a userform..as far as i understand

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    The title says 'only if 1 match'.

    Anyway, perhaps if we saw what you have it might help clarify things.

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  8. #8
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Yep Ok I'll do that in just a sec. Basically what it is is IF there is only 1 match I would like it to automatically chose that one for me.

  9. #9
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Sample1.xlsm Here it is

  10. #10
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Just add this piece of code after the loop

    If Me.ListBox1.ListCount = 1 Then
        Me.ListBox1.ListIndex = 0
        Selection.Value = Me.ListBox1.Value
    End If

  11. #11
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Thanks..what do you mean by the loop?

  12. #12
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Check the below code for details to know where to add the suggested code...

    Private Sub UserForm_Initialize()
    Dim rRng As Range, r As Range, rSortCr As String
    
    Set rRng = Sheet2.Range("a2:a5567")
    rSortCr = UCase(ActiveCell.Value)
    
    For Each r In rRng
        If InStr(UCase(r), rSortCr) Then Me.ListBox1.AddItem r.Value
    Next r
    
    If Me.ListBox1.ListCount = 1 Then
        Me.ListBox1.ListIndex = 0
        Selection.Value = Me.ListBox1.Value
    End If
    
    End Sub

  13. #13
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Thanks that works - how do I unload the userform after it's chosen the word for me?

  14. #14
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Ok so I can't get it to do that for me?

  15. #15
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Add this unload me in the below area

    If Me.ListBox1.ListCount = 1 Then
        Me.ListBox1.ListIndex = 0
        Selection.Value = Me.ListBox1.Value
        Unload Me
    End If
    And add this On Error Resume Next in the below area

    Sub Macro2()
    On Error Resume Next
    UserForm1.Show
    End Sub

  16. #16
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Excellent, perfect. Thanks very much for your time. Have some rep

  17. #17
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Edit VBA Userform code to run if only 1 entry that matches criteria

    Glad it helps you and thanks for the feedback and rep

+ 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] Userform multipage control exit events code execution not completed before next user entry
    By jane serky in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-10-2013, 04:39 AM
  2. Populating VBA userform and Fetch previously entered data edit and make new entry
    By vijaynadiad in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-07-2013, 11:59 AM
  3. VBA Code to Search/Edit Data Populated by Userform
    By pjohnson9 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-06-2011, 04:25 PM
  4. Return last entry in column that matches criteria
    By tomlancaster in forum Excel General
    Replies: 4
    Last Post: 01-22-2010, 10:27 AM
  5. Edit code to add extra criteria
    By duugg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2008, 11:34 AM

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