+ Reply to Thread
Results 1 to 16 of 16

Perform a Vlookup using a Form ComboBox as the Lookup Value

Hybrid View

  1. #1
    Registered User
    Join Date
    01-31-2014
    Location
    East Coast, England
    MS-Off Ver
    Excel 2010
    Posts
    44

    Perform a Vlookup using a Form ComboBox as the Lookup Value

    I am a poor student at VBA so this should be easy for you pro's out there.

    Below is as far as i have got. No errors are thrown up but it doesn't do as i expect it too.

    Any help appreciated.

    Thanks in advance.

    Option Explicit
    
    
    Private Sub ComboBox1_Change()
    
        Dim ws As Worksheet
        Dim ws1 As Worksheet
        Dim LastRow As Long
            Set ws = Worksheets("Tariffs")
            Set ws1 = Worksheets("Billing")
    
    On Error Resume Next
    
    LastRow = ws.Cells(Cells.Rows.Count, "A").End(xlUp).Row
    
    ws1.Cells("B2") = WorksheetFunction.VLookup(ComboBox1, ws.Range("Tariff2"), 2, False)
    
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    Hi,
    it's generally a bad practice to turn errors off, unless you have a reason to do so. it's certainly not wise when debug code. for start comment out

    On Error Resume Next
    so you will see if any error pops.
    do you have named range Tariff2 in sheet Tarrifs?
    If you are pleased with a member's answer then use the Star icon to rate it.

  3. #3
    Registered User
    Join Date
    01-31-2014
    Location
    East Coast, England
    MS-Off Ver
    Excel 2010
    Posts
    44

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    Okay thanks Buran.

    Yes the range Tariff2 is set in the Tariff sheet. In theory should the code I entered work? (am i barking up the right tree)

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

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    How are you populating the combobox?
    If posting code please use code tags, see here.

  5. #5
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    it looks right to me. see the attached example
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-31-2014
    Location
    East Coast, England
    MS-Off Ver
    Excel 2010
    Posts
    44

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    Quote Originally Posted by buran View Post
    it looks right to me. see the attached example
    Hi Buran,

    Yours works absolutely correct the only thing is you have defined the range and i need a dynamic range as some times there maybe more or less options in the table array on the lookup.

  7. #7
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    My code still works just fine if I define named range and use it in the vlookup.

  8. #8
    Registered User
    Join Date
    01-31-2014
    Location
    East Coast, England
    MS-Off Ver
    Excel 2010
    Posts
    44

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    Norie,

    The combobox is populated with the below code.


    Private Sub UserForm_Initialize()
      Dim T1 As Range
      Dim ws As Worksheet
      
        Set ws = Worksheets("Tariffs")
            
            For Each T1 In ws.Range("Tariff")
                
                Me.ComboBox1.AddItem T1.Value
                
            Next T1
                    
    End Sub

  9. #9
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    By the way, what is the scope of the defined named range on the worksheet Tarrifs. Is it the workbook or just the worksheet on which the range is?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Are the ranges Tariff and Tariff2 related in anyway?

  11. #11
    Registered User
    Join Date
    01-31-2014
    Location
    East Coast, England
    MS-Off Ver
    Excel 2010
    Posts
    44

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    Hi Norie,

    The Tariff range is A2 to A7 the Tariffs sheet.
    The Tariff2 range is A2 to D7 on the Tariffs sheet.

    Thanks for your time.

  12. #12
    Registered User
    Join Date
    01-31-2014
    Location
    East Coast, England
    MS-Off Ver
    Excel 2010
    Posts
    44

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    Hi Buran,

    The Range is A2 to D7 at the moment but i want it to be able to change to D8 if another is added etc.

    Thanks for your help.

  13. #13
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    Hi Richard.
    When I asked about the scope I mean what is the scope of the named range (where it is visible from). When you define a named range from Formulas->Name Manager, you can set the scope to workbook or to just one sheet. There is possibility that the named range Tariif2 scope is set to Tarrifs sheet, so it is not visible when you show form when Billing is active sheet.
    Consider to upload a sample workbook, otherwise it's a wild guess and may take a long time to solve the issue.

  14. #14
    Registered User
    Join Date
    01-31-2014
    Location
    East Coast, England
    MS-Off Ver
    Excel 2010
    Posts
    44

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    Hi Buran,

    The scope is set as workbook on both Tariff and Tariff2. I will upload the Workbook shortly first i need to get rid of all my embaressing workings out.

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

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    Richard

    Instead of VLookup you can use the ListIndex if the combobox to get the row and return the value from column B.
    
    Private Sub ComboBox1_Change()
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim LastRow As Long
    
        If ComboBox1.ListIndex = -1 Then Exit Sub
    
        Set ws = Worksheets("Tariffs")
        Set ws1 = Worksheets("Billing")
    
        LastRow = ws.Cells(Cells.Rows.Count, "A").End(xlUp).Row
    
        ws1.Range("B2").Value = ws.Range("Tariff2").Cells(Combobox1.ListIndex + 1,2).Value
    
    End Sub

  16. #16
    Registered User
    Join Date
    01-31-2014
    Location
    East Coast, England
    MS-Off Ver
    Excel 2010
    Posts
    44

    Re: Perform a Vlookup using a Form ComboBox as the Lookup Value

    I have it working perfectly now thanks guys. One of My Named Ranges was set wrong so i started again and its now doing as it should.

    I Set this as the named range TariffName =OFFSET(Tariffs!$A$2,0,0,COUNTA(Tariffs!$A$2:$A$200),1)
    and this as the vlookup range TariffRng =OFFSET(Tariffs!$A$2,0,0,COUNTA(Tariffs!$A$2:$A$200),4)

    The the following code

    Option Explicit
    Private Sub CBox_Tariff_Change()
    
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    
    Set ws = Sheets("Tariffs")
    Set ws1 = Sheets("Billing")
    
    ws1.Range("B1") = WorksheetFunction.VLookup(CBox_Tariff, Range("TariffRng"), 2, False)
    
    End Sub
    
    
    
    Private Sub UserForm_Initialize()
      Dim T1 As Range
      Dim ws As Worksheet
      
        Set ws = Worksheets("Tariffs")
            
            For Each T1 In ws.Range("TariffName")
                
                Me.CBox_Tariff.AddItem T1.Value
                
            Next T1
                    
    End Sub
    Thank you both for your help your suggestions helped me greatly to get the finished result.

+ 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. Replies: 1
    Last Post: 11-07-2014, 01:21 PM
  2. [SOLVED] Save Text box Value against lookup value of combobox in User form
    By rain4uu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-25-2013, 12:21 PM
  3. Replies: 2
    Last Post: 10-11-2012, 05:47 PM
  4. Calling a user form initialize sub when entering the form to populate combobox...
    By regupnorth in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-02-2012, 07:24 AM
  5. transferring combobox value from one user form to populate combobox on another
    By smartphreak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-05-2010, 10:12 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