Consulting

Results 1 to 12 of 12

Thread: Solved: Using Vlookup On UserForm

  1. #1
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location

    Solved: Using Vlookup On UserForm

    Hi All

    I have created a mock up UserForm which is attached - the problem I have is that the two text boxes that display the Site Info are restricted to Site 1 any suggestions on how I would be able to select any of the other sites 2 & 3 when necessary would be much appreciated.

    Regards

    Sooty8
    Attached Files Attached Files

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not sure I understand exactly what you mean/want.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    with the uf's current design, afaik, you can't.
    maybe you should add option buttons. or check boxes for multiple site values.

    column references are to 5th and 6th columns in B2:K20.
    [VBA]Tb5.Value = Application.VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 5, False)
    Tb6.Value = Application.VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 6, False)[/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location

    Vlookup2

    Hi Guys

    Have done a change on the userform and suggested a drop down to select which site to use - if you open the new form its all explained hopefully on there.

    Regards

    Sooty8
    Attached Files Attached Files

  5. #5
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    You could add a spin button. (See attachment)

    If the data was recognizable, you could put three option buttons in a frame and choose the one you wanted.

    Also, make your Rowsource range a dynamic named range. This way you can keep adding data without having to adjust the rowsource in the userform.

    Oh, and I changed the name of your userform to ufStart as Start is a Reserve word in VBA. (Possible conflicts) You should develop some naming convention standards for your code. I use uf for Userforms, cbox for Combobox, lbox for Listbox, txt for Textboxes, etc, plus the name of the field. For example, if you name your field txtTitle, then 6 months from now when you look at it, you will instantly know what that refers to, but you wouldn't remember what TB1 refers to. It's a little extra work on the front end, but it'll pay off down the road.
    Attached Files Attached Files

    David


  6. #6
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Here's a option button version.

    Also, something else to consider. If you are only using this userform to view records for copy to another sheet, use labels instead of textboxes to prevent inadvertently adding/deleting to the fields of the record.

    David
    Attached Files Attached Files

  7. #7
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location
    Hi David

    I prefer the spin button - can I ask a couple of questions is it possible to have a text box next to the spin button that on using the spin button and selecting a site the text box would display the site number as per the heading on sheet3 and also a check box that would lock it until I had finished with that site. The reason for this is there are 22 sites and each one has a minimum of 400 rows.

    Many Thanks for your help

    Regards

    Sooty8

  8. #8
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    This will error if you go past Site 3 since the vlookup range isn't defined past that.

    David
    Attached Files Attached Files
    Last edited by Tinbendr; 01-25-2011 at 08:22 AM. Reason: Added checkbox

  9. #9
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location
    Hi David

    Been in & out of meetings - sort of twigged the ranges and had a go at reassigning them - will have to be tomorrow early before I can do anything and after 10:00am I'm out most of the day will get back to you late afternoon if I miss the morning call.

    Thanks for the help

    Regards

    Sooty8

  10. #10
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location
    Hi David

    I have reset the column and row ranges and it is working OK - a problem at the moment is that even when locking the Site Info at Site3 and then changing Com3 to a new name it defaults each time to Site 1 is there a way to solve this.

    Regards

    Sooty 8

  11. #11
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    I'm not exactly sure what 'lock Site 3' means; for the current record... see below.
    [vba]Private Sub Com1_Change()
    With Application.WorksheetFunction
    Tb1.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 2, False)
    Tb2.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 1, False)
    Tb3.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 3, False)
    Tb4.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 4, False)
    If Not Me.CheckBox1 Then
    Tb5.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 5, False)
    Tb6.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 6, False)
    ufStart.lblSiteInfo.Caption = "Site info " & SpinButton1.Value
    End If
    End With
    End Sub[/vba]

    For another record (row), and use that record site 3 information? So if I had selected Bloggs, then Site 3, and I changed to Smith, you want the info for Smith-Site 3?
    [VBA]Private Sub Com1_Change()
    With Application.WorksheetFunction
    Tb1.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 2, False)
    Tb2.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 1, False)
    Tb3.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 3, False)
    Tb4.Value = .VLookup(Com1.Value, Worksheets("Sites").Range("B2:K10"), 4, False)
    SiteInfo SpinButton1.Value
    ufStart.lblSiteInfo.Caption = "Site info " & SpinButton1.Value
    End With
    End Sub[/VBA]
    Last edited by Tinbendr; 01-26-2011 at 06:57 AM.

  12. #12
    VBAX Contributor
    Joined
    Aug 2007
    Posts
    188
    Location
    Hi David

    Thanks for your help - now does exactly what I wanted

    Regards

    Sooty8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •