+ Reply to Thread
Results 1 to 3 of 3

Writing Function using vlookup

Hybrid View

  1. #1
    Registered User
    Join Date
    09-25-2008
    Location
    Sydney
    Posts
    4

    Writing Function using vlookup

    Hi,

    I'm trying to write a simple funtion (my first!).

    Function is:
    Public Function ytdvol(x1, x2)
        x3 = Application.VLookup(x1, PriceHist, 3, False).Value
        x4 = Application.VLookup(x2, PriceHist, 3, False).Value
    
    ytdvol = x3 + x4
    End Function
    There is no problem with the vlookup range etc. I get a #VALUE. Any help would be great!

    Thanks,
    Ben.
    Last edited by Paul; 09-25-2008 at 11:54 PM.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887
    Hi Perdben, welcome to the forum. Please take a moment to read our forum rules. In the future, please wrap your code using the # button in the post editing window. I've done it for you this time, but future posts may be locked.

    As for your issue, when referring to a named range, use Range("Your_Named_Range"), qualify the variable types you're passing to the function, and use Application.WorksheetFunction.[some_function_here].

    For example:
    Public Function ytdvol(x1 As Range, x2 As Range)
        x3 = Application.WorksheetFunction.VLookup(x1, Range("PriceHist"), 3, False)
        x4 = Application.WorksheetFunction.VLookup(x2, Range("PriceHist"), 3, False)
        ytdvol = x3 + x4
    End Function
    Be sure to put this into a standard Module, not in your Worksheet or ThisWorkbook code window.

  3. #3
    Registered User
    Join Date
    09-25-2008
    Location
    Sydney
    Posts
    4
    OK, that worked well. Thanks very much!

+ 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. Loop function with vlookup
    By Engineers08 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-20-2012, 11:28 PM
  2. Vlookup Function over multiple sheets
    By casdaq in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-15-2008, 02:20 PM
  3. Sum using VLookup Function
    By casdaq in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-04-2008, 06:03 PM
  4. Vlookup function.
    By flyandcrash in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-31-2008, 10:23 PM
  5. vlookup function-for expanded payroll
    By cheggie1 in forum Excel General
    Replies: 4
    Last Post: 02-21-2007, 12:00 AM

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