+ Reply to Thread
Results 1 to 10 of 10

Thread: VLookup to populate useform textbox

  1. #1
    Registered User
    Join Date
    12-29-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    20

    VLookup to populate useform textbox

    Hi,

    I'm trying to use the following code to populate a textbox in a userform based on the selection made in a combobox. The "MerchPrices" in the the code is a defined name for a two column selection. The first column fills the combobox and the second is intended to fill the textbox.


    Private Sub Combobox1_Change()
    
    Textbox1.Locked = False
    Textbox1.Value = WorksheetFunction.VLookup(CLng(Combobox1.Value), Range("MerchPrices"), 2, 0)
    Textbox1.Locked = True
    
    End Sub
    When I run the code I get a "Run-time error '13': Type mismatch". I don't know how to troubleshoot this problem. The array that fills the combobox with items matches up with the "MerchPrices" array, so I don't think there is a problem with the arrays mismatching.


    Thanks,
    Last edited by Einstahb; 10-07-2011 at 11:12 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: VLookup to populate useform textbox

    Hello Einstahb,

    The problem maybe you are not using the function correctly. For example, let's say the 2-D table has 2 columns. The first (left most) is "Item" and the second is "Price" and table looks like this and is named "Current_Pricing".

    Item    Price
    Item A  $12.50
    Item B  $7.95
    Item C  $14.00
    Item D  $3.75
    Item E  $5.25

    The function must match the look up value in the "Item" column. When a match is found, the column index (third argument) will return the value from that column in the same row as a match. If you have...
        Price = WorksheetFunction.VLookup("Item C", Range("Current_Pricing", 2, 0)

    The price returned will $14.00 for Item C.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: VLookup to populate useform textbox

    I wouldn't use a Lookup. You might also consider a Label instead of a TextBox, then you don't need to Lock/Unlock. This code assumes the list has a header row in Row 1. The ListIndex starts at zero so adding 2 allows for the header Row & gets the row number that corresponds

    Option Explicit
    
    
    Private Sub Combobox1_Change()
        With Me
            .TextBox1.Locked = False
            .TextBox1.Value = Sheet1.Range("MatchPrices").Cells(.ComboBox1.ListIndex + 2, 2).Value
            .Label1.Caption = Sheet1.Range("MatchPrices").Cells(.ComboBox1.ListIndex + 2, 2).Value
            .TextBox1.Locked = True
        End With
    End Sub
    
    
    Private Sub UserForm_Initialize()
        Me.ComboBox1.List = Sheet1.Range("MatchPrices").Columns(1).Offset(1, 0).Value
        Me.TextBox1.Locked = True
    End Sub
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  4. #4
    Registered User
    Join Date
    12-29-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    20

    Re: VLookup to populate useform textbox

    Thank you royUK! That worked perfectly. And yes, I was hoping to use a label instead of a textbox but I didn't know how to go about doing it. This has made things a lot easier!

    Thanks again

  5. #5
    Registered User
    Join Date
    12-29-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    20

    Re: VLookup to populate useform textbox

    I have a quick additional question to ask with regard to royUK's proposed function - is there a fast and easy way to change the number formatting of the value displayed in the label to match the number formatting that I had in my list? For instance, right now it shows 10 when I select an item in the combobox and I would like it to display $10.00. I could type these values into the list but it would be much easier if a simple formatting could be applied to the label. Is that possible?

  6. #6
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: VLookup to populate useform textbox

    Try this

    Option Explicit
    
    
    Private Sub Combobox1_Change()
        With Me
            .TextBox1.Locked = False
            .TextBox1.Value = Sheet1.Range("MatchPrices").Cells(.ComboBox1.ListIndex + 2, 2).Value
            .Label1.Caption = format(Sheet1.Range("MatchPrices").Cells(.ComboBox1.ListIndex + 2, 2).Value,,"Currency"))
            .TextBox1.Locked = True
        End With
    End Sub
    
    
    Private Sub UserForm_Initialize()
        Me.ComboBox1.List =Sheet1.Range("MatchPrices").Columns(1).Offset(1, 0).Value
        Me.TextBox1.Locked = True
    End Sub
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  7. #7
    Registered User
    Join Date
    12-29-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    20

    Re: VLookup to populate useform textbox

    I had to remove a comma and an extra parentheses to get the following, but it worked exactly how I wanted thereafter.

    .Label1.Caption = format(Sheet1.Range("MatchPrices").Cells(.ComboBox1.ListIndex + 2, 2).Value,"Currency")
    Thanks again!

  8. #8
    Forum Guru mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    2,998

    Re: VLookup to populate useform textbox

    Once the Userform is loaded, I wouldn't refer to the sheet at all

    Private Sub ComboBox1_Change()
        With ComboBox1
            If -1 < .ListIndex Then TextBox1.Text = .Value
        End With
    End Sub
    
    Private Sub UserForm_Initialize()
        With ComboBox1
            .ColumnCount = 2
            .ColumnWidths = ";0"
            .TextColumn = 1
            .BoundColumn = 2
            .List = Range("MatchPrices").Resize(,2).Value
        End With
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Registered User
    Join Date
    12-29-2010
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    20

    Re: VLookup to populate useform textbox

    Is there a significant advantage to not referring to the sheet? I'm guessing it may save a small fraction of running time. Is there anything else?

  10. #10
    Forum Guru mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    2,998

    Re: VLookup to populate useform textbox

    There isn't any great advantage in this application, where the whole range is loading into the ComboBox.
    But the approach is most useful when one fills a combobox with filtered items from a data set. (e.g. all the items that are in stock). If you have a fiddely filter for the data base, then all the fiddling is concentrated in one place in the code, the Load Combobox section, and doesn't propegate to the rest of it.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0