Consulting

Results 1 to 9 of 9

Thread: vlookup worksheet formula error: sub or function not defined?

  1. #1

    vlookup worksheet formula error: sub or function not defined?

    Hi all,

    I've searched the forum for a response on this, but I don't think it's because of vlookup itself - I must be making some kind of basic error I'm not seeing .

    Background: I have a form with a textbox, and want to use vlookup each time the value of the textbox changes in order to check whether the value is one that exists on a background worksheet.

    Problem: Even though vlookup is shown as a worksheet function available in VBA, I get 'compile error: sub or function not defined'.

    Sub txt_AcYear_Change()
    Dim AcademicYear As String
        AcademicYear = VLookup(txt_AcYear.Text, Worksheets("_background").Range("C:C"), 1, False)
        lbl_GetIsValid.Caption = AcYear 'This will show whether the textbox value ' has been found
     
    End Sub
    ...just to confirm, this is the code attached to the textbox change event.

    Can anyone see what I'm doing wrong?

    Thanks for reading,

    Fray

    UPDATE: Also, changing it to
     AcademicYear = Application.VLookup(txt_AcYear.Text, Worksheets("_background").Range("C:C"), 1, False)
    gives me a type mismatch error instead, in case anyone was going to suggest that.
    Last edited by crazyfray; 07-21-2007 at 09:49 AM.

  2. #2
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Try using this :[VBA]Application.WorksheetFunction.VLookup[/VBA]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Or even just Application.VLookup
    ____________________________________________
    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

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    If you are getting a type mismatch error then it's probably because the Vlookup isn't finding a match and is returning N/A.

  5. #5
    Quote Originally Posted by Charlize
    Try using this :[vba]Application.WorksheetFunction.VLookup[/vba]
    Quote Originally Posted by xld
    Or even just Application.VLookup
    Quote Originally Posted by Norie
    If you are getting a type mismatch error then it's probably because the Vlookup isn't finding a match and is returning N/A.
    From your responses, seems like I had 2 problems - not calling the vlookup properly, and when I did, the type mismatch (thanks Norie) kicks in.

    I don't want the code to stop at this point if no value is matched, so I guess I'll have to see what I can do. I'm sure there must be a way around it, else vlookup would be pretty useless in any situation where you can't guarantee a valid return.

    Thanks - will post back here when I'm at my comp to try these out.

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    On Error Resume Next could be one solution.

    Another could be to use a formula like CountIf to see if the value actually exists before using the VLookup.

    CountIf will return 0 not an error if the value isn't found.

    Another solution might be to use a different method.

    I'm thinking a combobox or listbox instead of a textbox.

    Note the root cause of the problem could possibly be the use of the change event - that get's triggered every time the textbox changes.

    So you might be ending up trying to look up partial values.

    Eg let's say you are looking for Value1

    As you type in the textbox the code will be triggered and the first thing to be looked for would be V.

  7. #7
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    On Error Resume Next prior to performing the Vlookup (you could change this for a Match function or, better, to a VBA Find in which case you won't return an error if the value isn't found), and then test for an error value returned eg:

    On Error Resume Next
    AcademicYear = Application.VLookup(txt_AcYear.Text, Worksheets("_background").Range("C:C"), 1, False)
    If Err=0 Then 'value found
    'blah blah blah

  8. #8
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Or declare your variable as a variant, use Application.Vlookup and then check for [VBA]If IsError(varAcademicYear) Then[/VBA]...
    Just as an alternative. Since you don't actually need VLookup, I would go with Application.Countif though.
    Regards,
    Rory

  9. #9
    Apologies for huge delay in responding here - several deadlines in a row make for me forgetting about this thread (very unfair of me)! Thanks to all those who have replied.

    Looking back at this, I changed this to instead work from offsets instead of vlookup. So, it searches for the input of AcademicYear, knows that the table of data starts 2 cells below that and offsets for the varying values (would post it, but not really relevant to the problem I was having).

    I'd mark this as solved, but I remade it from the ground up instead, and can't really help anyone that encounters the same problem (though I've used Excel worksheet functions in VBA and not had a problem, which makes me certain it was user error :P ).

    /me going to go and post a couple of replies to absolve myself for not replying...

Posting Permissions

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