+ Reply to Thread
Results 1 to 11 of 11

VBA custom function, Expected List Separator or )

Hybrid View

  1. #1
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Question VBA custom function, Expected List Separator or )

    Hello, so I've never written a custom function before and I'm doing something that should be quite simple.

    I'm taking the VLOOKUP function, and creating a new function that does the *same* exact thing, except it simplifies the typing process. Take a look.

    Function cvl(name, range, column)
    
    Dim name As String
    Dim range As range
    Dim column As Integer
    
    cvl(name, range, column) = application.VLOOKUP("*"&name&"*,"&range&","&column&",FALSE)"
    
    End Function
    The goal is just to have a shorter function name, and to not have to type the quotation marks with the asterisks, I only won't need to type FALSE at the end of it every time. Thinking on it now I may even try to default the range as well.

    Either way, it doesn't seem to like my commas but I'm not sure why. the VLOOKUP function is working perfectly fine in my excel sheet when typed like this (in the cell that is): "=VLOOKUP("*itemname*",A1:D26,4,FALSE)" so I'm not sure why that isn't translating nicely to the function.

  2. #2
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    VBA custom function, Expected List Separator or )

    Function cvl(name As String, _
                        range As range, _
                        column As Integer) as variant
    
    name = "*" & name & "*"
    
    cvl = application.VLOOKUP(name,range,column,FALSE)
    
    End Function



    Sent from my iPhone using Tapatalk
    Last edited by coolblue; 06-14-2014 at 09:54 PM.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA custom function, Expected List Separator or )

    When writing UDFs one doesn't include the arguments in the final assignment of the value of the function.
    Also, the data type of the arguments is given in the function declaration line, not a Dim statement.
    I'm also hesitant to use Range, Column and other common names of properties as the arguments to UDF's.

    Function cvl(aName As String, myRange As Range, myColumn As Integer) As Variant
    
        cvl = Application.VLOOKUP("*" & aName & "*", myRange, myColumn, False)
    
    End Function
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: VBA custom function, Expected List Separator or )

    I think this is covered but this is my try:

    Public Function cvl(ByVal name As Variant, ByVal rRange As range, ByVal column As Integer, Optional ByVal match As Boolean = False) As Variant
    
    cvl = WorksheetFunction.VLookup("*" & name & "*", rRange, column, match)
    
    End Function

  5. #5
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Re: VBA custom function, Expected List Separator or )

    The code I ended up going with was this, and it works fine

    Function cvl(aName As String, myRange As range, myColumn As Integer) As Variant
    
    cvl = Application.VLookup("*" & aName & "*", myRange, myColumn, False)
    
    End Function
    I decided that I actually want to make the range and column static as well. I tried a few different things but none of them have worked so far. This was my last attempt but it just returns 0 all the time.



    Function cvl(aName As String) As Variant
    
    cvl = Application.VLookup("*" & aName & "*", range(A1, D40), 4, False)
    
    End Function
    Thoughts?
    Thanks so much!

  6. #6
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Re: VBA custom function, Expected List Separator or )

    Double posted. Not sure how to delete this post though.
    Last edited by NicholasL; 06-15-2014 at 08:11 AM. Reason: double post

  7. #7
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: VBA custom function, Expected List Separator or )

    Hi,

    <issing the correct range notation

    Function cvl(aName As String) As Variant
    
    cvl = Application.VLookup("*" & aName & "*", range("A1:D40"), 4, False)
    
    End Function
    Thanks

  8. #8
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Re: VBA custom function, Expected List Separator or )

    Ahhhhh, I see.
    Great, thanks to all who contributed!

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA custom function, Expected List Separator or )

    My thinking is that hard coded values in a function can be a pain down the line. Rather than have
    =cvl("Smith") in a cell, I'd prefer to use the UDF

    Function containString(aString as String) As String
        containString = "*" & aString & "*"
    End Function
    and put =VLOOKUP(containString("Smith"), Range($A$1:$D$40), 4, False) in the cell.

  10. #10
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Re: VBA custom function, Expected List Separator or )

    It's some solid advice but what doesn't work for me is just the length of the formula. The workbook is personal, by which I mean it's not for work or a client or anything like that, and the way I have it set up I don't foresee ever needing to change those other values (and if I do, I can effectively change it for *every* cell containing my UDF by making a slight edit to the code, rather than changing every cell containing one of these -- which is going to be a lot).

  11. #11
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: VBA custom function, Expected List Separator or )

    I totally get what mike says. We think we have all under control but later on a few months from now something starts failing and we cannot find what its and we already forgotten that we made that range hard coded. by the way your VLOOKUP formula is pretty small, I wonder if you will add more to it.

    Good Luck

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Complie Error: Expected: list separator or (
    By Poornima Rajgopal in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-07-2013, 12:32 PM
  2. [SOLVED] Continue code on next line: Compile Error: expected list separator or )
    By ike609 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-05-2012, 08:28 AM
  3. compile error – expected : list separator or )
    By mdavid800 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-27-2011, 04:59 PM
  4. Replies: 1
    Last Post: 03-23-2006, 01:10 PM
  5. [SOLVED] Comma separator for custom Currency
    By Bald Eagle in forum Excel General
    Replies: 0
    Last Post: 01-16-2006, 07:20 PM

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.6.0 RC 1