Consulting

Results 1 to 17 of 17

Thread: Solved: VBA VLookup Help (Excel '97)

  1. #1
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location

    Solved: VBA VLookup Help (Excel '97)

    Hi there,

    I trying to setup a VLOOKUP in a Userform.

    [vba]
    Dim nlook as Variant
    Dim dLook As Variant
    Dim sLook As Variant
    Set ws = Worksheets("Lookups")
    nLook = Application.VLookup(Me.txtDealerNo.Value, ws.Range("V14:X1033"), 1, True)
    dLook = Application.VLookup(Me.txtDealerNo.Value, ws.Range("V14:X1033"), 2, True)
    sLook = Application.VLookup(Me.txtDealerNo.Value, ws.Range("V14:X1033"), 3, True)
    [/vba]

    Now, when the user enters the dealer number in a particular field it should then populate other fields :

    [vba]
    Private Sub txtDealerNoSendTo_Change()
    Call sDName
    End Sub

    Private Sub txtDealerNoSendTo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Call sDName
    End Sub

    Private Sub sDName()
    If Me.txtDealerNo.Value = nLook Then
    Me.txtDealerName.Value = dLook

    ElseIf Me.txtDealerNo.Value = "" Then
    Me.txtDealerName.Value = ""

    End If
    End Sub
    [/vba]

    Where am I goinf wrong as when I enter a number into the field nothing happens. Would any be able to point me in the right direction.

    Thanks,
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Where is the lookup code stored?
    ____________________________________________
    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 Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Hi xld,

    Dim nlook As Variant
    etc

    Declared in the initalise routine.
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Well, surely, at that point, the texboxes are blank.
    ____________________________________________
    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

  5. #5
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    That is a very good point xld!

    How about this :-

    [VBA]
    '---------------------------------------'
    '-- Dealer No Field --'
    '---------------------------------------'
    Private Sub txtDealerNo_Change()
    Call sDName
    End Sub

    Private Sub txtDealerNo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Call sDName
    End Sub
    Private Sub sDName()
    Dim nLook As Variant
    Dim dLook As Variant
    Dim sLook As Variant
    nLook = Application.VLookup(Me.txtDealerNo.Value, ws.Range("V14:X1033"), 1, True)
    dLook = Application.VLookup(Me.txtDealerNo.Value, ws.Range("V14:X1033"), 2, True)
    sLook = Application.VLookup(Me.txtDealerNo.Value, ws.Range("V14:X1033"), 3, True)
    If Me.txtDealerNo.Value = nLook Then
    Me.txtDealerName.Value = dLook

    ElseIf Me.txtDealerNo.Value = "" Then
    Me.txtDealerName.Value = ""

    End If
    End Sub
    [/VBA]

    It now errors with 'Object Required' and highlights :
    nLook = Application.VLookup(Me.txtDealerNo.Value, ws.Range("V14:X1033"), 1, True)
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Okqy; qt this point you hqvenùt defined zs:
    ____________________________________________
    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

  7. #7
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Quote Originally Posted by xld
    Okqy; qt this point you hqvenùt defined zs:
    Having a bad keyboard moment xld?

    I didn't think i'd need to define 'ws' again as the userform_initalize is a public sub.
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Wow, my keyboard mapping is well and truly screwed. Better re-boot.

    Does your comment mean that you don't think ws is a problem? It won't be if ws is a module scope variable.
    ____________________________________________
    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

  9. #9
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Quote Originally Posted by xld
    Wow, my keyboard mapping is well and truly screwed. Better re-boot.

    Does your comment mean that you don't think ws is a problem? It won't be if ws is a module scope variable.
    Ok, the 'ws' is a problem, i have now added the following :

    Set ws = Worksheets("Lookups")

    Now, the code errors :- 'Could not set the property value. Type mismatch'
    Highlights :- Me.txtDealerName.Value = dLook

    The actual lookup list is a mixture of just numbers (12345), numbers and text (12345AA) and text and numbers (A1234).
    I have tried to set this list to be formatted as text, numbers and general and still have the error.

    Edit : VBA password = 0range, read-only= k1wifruit
    (Spreadsheet is proteced but not with a password, look for the small up/down arrows to the far far right (Column S)
    Last edited by phendrena; 02-27-2009 at 01:21 AM. Reason: added file
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  10. #10
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Hi,

    Anyone able to help me with this one please?

    Thanks,
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  11. #11
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Ok, instead of the above i've tried the following code :
    [VBA]Private Sub txtDealerNo_Change()
    Set ws = Worksheets("Lookups")
    txtDealerName.Value = WorksheetFunction.VLookup(txtDealerNo.Value, ws.Range("W14:Y639"), 2, 0)
    End Sub[/VBA]
    I now get the error : Unable to get the Vlookup property of the WorksheetFunction class

    Any suggestions on how I can get this to work?

    Thanks,
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  12. #12
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    I'm such a pleb. I really am.

    I have solved this problem by adding an error routine
    On Error Resume Next

    this has solved the problem and now works.
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How does that help, it just skips the error, so it just avoids the problem?
    ____________________________________________
    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

  14. #14
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Quote Originally Posted by xld
    How does that help, it just skips the error, so it just avoids the problem?
    I can see your logic.
    The error occurs as soon as you start to type into the field so by adding the resume next it doesn't error and does the lookup correctly when the field is completed. I agree it's not a very good way to solve the problem but as a quick solution it'll do for now.
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  15. #15
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try using an AfterUpdate event instead of a Change event
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Or the Exit event.

    You should use the change event if you want to check after every character, otherwise validate using a button is how I do it, what we call cross-validation, validating across the form.
    ____________________________________________
    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

  17. #17
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Hi,

    I've changed it to an AfterUpdate event :
    [vba]Private Sub txtDealerNo_AfterUpdate()
    Set ws = Worksheets("Lookups")
    On Error GoTo ErrHandler
    txtDealerName.Value = Application.WorksheetFunction.VLookup(txtDealerNo.Value, ws.Range("W14:Y1140"), 2, False)
    cboScheme.Value = Application.WorksheetFunction.VLookup(txtDealerNo.Value, ws.Range("W14:Y1140"), 3, False)
    Exit Sub
    ErrHandler:
    MsgBox "Dealer Not Found"
    End Sub[/vba] All working nicely.
    Thank you for your suggestions and assistance.
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

Posting Permissions

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