+ Reply to Thread
Results 1 to 6 of 6

match in one range to extract value from another

Hybrid View

  1. #1
    Registered User
    Join Date
    03-20-2012
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    24

    match in one range to extract value from another

    I'm sure this can be done better. Thisdate is a set of dates in, say a1 to a100. in b1 to b100 there are values [the val range]. I'm trying to return the value in val that corresponds to thisdate matching startdate.

    this code produces an error.


    Function INIT(THISDATE As Range, STARTDATE As Date, VAL As Range)
    Dim C, B
    Dim ICOUNT As Integer, MAX As Integer
    Dim ARR1()
    Dim ARR2()
    MAX = THISDATE.Count
    ReDim ARR1(1 To MAX)
    ReDim ARR2(1 To MAX)

    ARR1 = Array(THISDATE)
    ARR2 = Array(VAL)
    For ICOUNT = 1 To MAX
    If DateValue(ARR1(ICOUNT)) = STARTDATE Then
    INIT = ARR2(ICOUNT)
    Exit Function
    End If
    Next ICOUNT
    End Function

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: match in one range to extract value from another

    Which line is the error?

  3. #3
    Registered User
    Join Date
    03-20-2012
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: match in one range to extract value from another

    Quote Originally Posted by StevenM View Post
    Which line is the error?
    I don't know. The spreadsheet only shows #Value!

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: match in one range to extract value from another

    jalea148,

    In the future, please wrap your code in code tags per the forum rules (link in my sig for how).
    As to your question, I'm not sure why you're trying to use a UDF when Excel has several native functions that can perform what you're trying to do. The most popular for this task is the VLookup() function. Using your stated example, it would be:

    =VLOOKUP(STARTDATE,A1:B100,2,FALSE)
    Where STARTDATE is the date to match.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  5. #5
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: match in one range to extract value from another

    As for your code.
    Delete the ReDim statements.
    Omit the Array() so that: ARR1 = THISDATE and ARR2 = VAL
    And then, assuming the ranges are columns use: DateValue(ARR1(ICOUNT, 1)) = STARTDATE Then
    And: INIT = ARR2(ICOUNT, 1)

    The point here is that you can write a range to a variant, and the variant will become a two-dimensional array (even if the range is only one row, or only one column).

  6. #6
    Registered User
    Join Date
    03-20-2012
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    24

    Thumbs up Re: match in one range to extract value from another

    Quote Originally Posted by StevenM View Post
    As for your code.
    Delete the ReDim statements.
    Omit the Array() so that: ARR1 = THISDATE and ARR2 = VAL
    And then, assuming the ranges are columns use: DateValue(ARR1(ICOUNT, 1)) = STARTDATE Then
    And: INIT = ARR2(ICOUNT, 1)

    The point here is that you can write a range to a variant, and the variant will become a two-dimensional array (even if the range is only one row, or only one column).

    Sweet - works fine. Thanx

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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