+ Reply to Thread
Results 1 to 5 of 5

Use textbox input and VLookup to update another textbox on same userform

Hybrid View

  1. #1
    Registered User
    Join Date
    04-20-2011
    Location
    Richland, WA
    MS-Off Ver
    Excel 2003/2007
    Posts
    31

    Use textbox input and VLookup to update another textbox on same userform

    I'm trying to use a textbox entry on a userform to populate another textbox (or label) on the same form based on a Vlookup to a dynamic named range. I've tried doing this a number of times and ways with the Vlookup worksheetfunction and all resulted in a "1004" error. I would like to populate CanType from Can and GrossWt from Truck (named ranges CanInfo and TruckInfo). Overall goal is to drive most of the database values from code and not using lookups in individual cells - file size bloat.

    Need push in right (or at least better) direction.
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Use textbox input and VLookup to update another textbox on same userform

    Because Can and Truck are integers on the LookUpLists sheet, you need to convert the values of the textboxes to integers (using CInt).

    Try this:

    Private Sub cmdEnter_Click()
    
    On Error Resume Next
    
    If Me.txtCan.Text = "" Then
        MsgBox "Please enter a Can.", vbCritical
        Me.txtCan.SetFocus
        Exit Sub
    End If
    If Me.txtTruck.Text = "" Then
        MsgBox "Please enter a Truck.", vbCritical
        Me.txtTruck.SetFocus
        Exit Sub
    End If
    
    Me.txtCanType.Text = Application.VLookup(CInt(Me.txtCan.Text), Range("CanInfo"), 3, 0)
    Me.txtGW.Text = Application.VLookup(CInt(Me.txtTruck.Text), Range("TruckInfo"), 3, 0)
    
    End Sub

    Dion

  3. #3
    Registered User
    Join Date
    04-20-2011
    Location
    Richland, WA
    MS-Off Ver
    Excel 2003/2007
    Posts
    31

    Re: Use textbox input and VLookup to update another textbox on same userform

    Works great! Thank you!

    Now I can finish the data entry form and merge it with the larger dataset and other modules. Next pint is on me.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Use textbox input and VLookup to update another textbox on same userform

    I wouldn't use VLOOKUP in this situation, Excel has a VBA method called .Find.

    However, to avoid user error why not use CombBoxes for Can & Truck? This would improve input & make the rest simpler

    '---------------------------------------------------------------------------------------
    ' Module    : UserForm1
    ' Author    : Roy Cox
    ' Website   : for more examples and Excel Consulting
    ' Date      : 13/05/2011
    ' Purpose   : Add information to UserForm based on CombBox selection
    '---------------------------------------------------------------------------------------
    
    Option Explicit
    
    
    Private Sub cboCan_Change()
        Me.txtCanType.Value = Range("caninfo").Cells(Me.cboCan.ListIndex + 1, 3)
    End Sub
    
    Private Sub cboTruck_Change()
        With Me
            .txtGW.Value = Range("TruckInfo").Cells(.cboTruck.ListIndex + 1, 3)
            .txtNW.Value = Range("TruckInfo").Cells(.cboTruck.ListIndex + 1, 4)
            .txtTons.Value = Range("TruckInfo").Cells(.cboTruck.ListIndex + 1, 5)
            .txtTonsOU.Value = Range("TruckInfo").Cells(.cboTruck.ListIndex + 1, 6)
        End With
    End Sub
    
    
    Private Sub cmdExit_Click()    '
    ' exit userform without saving
        Unload Me
    End Sub
    
    
    Private Sub UserForm_Initialize()
    ' enter startup values
        With Me
            .txtDate.Value = Format(Now(), "mm/dd/yyyy")
            .cboSite.ListIndex = 0
            .cboTruck.List = Range("Trucks").Value
            .cboCan.List = Range("caninfo").Columns(1).Value
        End With
    End Sub
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    04-20-2011
    Location
    Richland, WA
    MS-Off Ver
    Excel 2003/2007
    Posts
    31

    Re: Use textbox input and VLookup to update another textbox on same userform

    Roy

    Thank you.

    I will give that a try also ... difference between brute force and finesse.

    Bob

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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