+ Reply to Thread
Results 1 to 12 of 12

VLOOKUP searching array in memory

  1. #1
    Registered User
    Join Date
    06-08-2005
    Posts
    67

    VLOOKUP searching array in memory

    I am trying to compare a date to an internal array of dates that represent holidays.
    I want to check if the date is a holiday or not.

    My code is

    holiday_fnd = Application.VLookup(process_date, holiday_arr, 1, false)
    if iserror(holiday_fnd) then ' not a holiday
    do something
    else
    do something else
    end if

    process_date is defined as date, holiday_arr is an array

    For every process_date I get error 2042 (not found).

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VLOOKUP searching array in memory

    Is process_date a serial date and also the array dates serial dates?

    I suspect that either the process_date date is text that "looks" like a date but it's not a serial date. Or the array dates are not serial dates. Can't be more specific without knowing the nature of your data.

    Edit:
    If you declared process_date as a Date, then likely the array dates are text-type dates.
    Last edited by AlphaFrog; 04-19-2018 at 06:27 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    06-08-2005
    Posts
    67

    Re: VLOOKUP searching array in memory

    process_date has a format of mm/dd/yyyy, as do the dates in the array

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VLOOKUP searching array in memory

    If all the dates are serial dates, date format is not relevant.

    How do you populate the array?

  5. #5
    Registered User
    Join Date
    06-08-2005
    Posts
    67

    Re: VLOOKUP searching array in memory

    I populate the array though logic that goes through each of the holidays for the year (New Years Day, Presidents Day, 4th of July, etc), and determine the date of the holiday. That date is then added as the next element in the array. I know the array contains the dates, as I debug.print the array to ensure it is populated.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VLOOKUP searching array in memory

    Quote Originally Posted by mwc0914 View Post
    I populate the array though logic that goes through each of the holidays for the year (New Years Day, Presidents Day, 4th of July, etc), and determine the date of the holiday. That date is then added as the next element in the array. I know the array contains the dates, as I debug.print the array to ensure it is populated.
    can you show the code for that?

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VLOOKUP searching array in memory

    Another thought is if holiday_arr is a 1-dimensional array, VLookup may not work. I don't know this for sure.

    Maybe try the Match function instead.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-08-2005
    Posts
    67

    Re: VLOOKUP searching array in memory

    This is a called subroutine. holiday_start_year is the year for the holidays.

    Sub build_holiday_array(idx, holiday_start_year, holiday_arr, holiday_ind)
    ' Build holiday array for each book year

    ' New Years Day (Jan 1)
    If Weekday("1/1/" & holiday_start_year) = 1 Then
    holiday_arr(idx) = "1/2/" & holiday_start_year
    Else
    holiday_arr(idx) = "1/1/" & holiday_start_year
    End If

    ' MLK day (3rd Mon of Jan)
    idx = idx + 1
    nth = 3
    dow = 2
    holiday_arr(idx) = DateSerial(holiday_start_year, 1, 1 + 7 * nth) - Weekday(DateSerial(holiday_start_year, 1, 8 - dow))

    ' Presidents day (3rd Mon of Feb)
    idx = idx + 1
    nth = 3
    dow = 2
    holiday_arr(idx) = DateSerial(holiday_start_year, 2, 1 + 7 * nth) - Weekday(DateSerial(holiday_start_year, 2, 8 - dow))

    ' Memorial day (Last Mon of May)
    idx = idx + 1
    start_date = "5/1/" & holiday_start_year
    holiday_arr(idx) = CDate(Application.EoMonth(start_date, 0) - Weekday(Application.EoMonth(start_date, 0) + 6) + 1)

    ' Independence Day (Jul 4)
    idx = idx + 1
    If Weekday("7/4/" & holiday_start_year) = 1 Then
    holiday_arr(idx) = "7/5/" & holiday_start_year
    Else
    holiday_arr(idx) = "7/4/" & holiday_start_year
    End If

    ' Labor day (First Mon of Sep)
    idx = idx + 1
    nth = 1
    dow = 2
    holiday_arr(idx) = DateSerial(holiday_start_year, 9, 1 + 7 * nth) - Weekday(DateSerial(holiday_start_year, 9, 8 - dow))

    ' Columbus day (2nd Mon of Oct) Loomis/Bank only
    If holiday_ind = 1 Or holiday_ind = 2 Then
    idx = idx + 1
    nth = 2
    dow = 2
    holiday_arr(idx) = DateSerial(holiday_start_year, 10, 1 + 7 * nth) - Weekday(DateSerial(holiday_start_year, 10, 8 - dow))
    End If

    ' Veterans day (Nov 11)
    idx = idx + 1
    If Weekday("11/11/" & holiday_start_year) = 1 Then
    holiday_arr(idx) = "11/12/" & holiday_start_year
    Else
    holiday_arr(idx) = "11/11/" & holiday_start_year
    End If

    ' Thanksgiving and day after (4th Thurs/Fri of Nov)
    idx = idx + 1
    nth = 4
    dow = 5
    holiday_arr(idx) = DateSerial(holiday_start_year, 11, 1 + 7 * nth) - Weekday(DateSerial(holiday_start_year, 11, 8 - dow))

    ' PG&E only for day after Thanksgiving
    If holiday_ind = 0 Then
    idx = idx + 1
    dow = 6
    holiday_arr(idx) = DateSerial(holiday_start_year, 11, 1 + 7 * nth) - Weekday(DateSerial(holiday_start_year, 11, 8 - dow))
    End If

    ' Christmas (Dec 25)
    idx = idx + 1
    If Weekday("12/25/" & holiday_start_year) = 1 Then
    holiday_arr(idx) = "12/26/" & holiday_start_year
    Else
    holiday_arr(idx) = "12/25/" & holiday_start_year
    End If

    End Sub

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VLOOKUP searching array in memory

    Some of those dates in the array are just text. Like this one...
    Please Login or Register  to view this content.
    This would make it a serial date...
    Please Login or Register  to view this content.
    Others are serial dates.
    Last edited by AlphaFrog; 04-19-2018 at 07:34 PM.

  10. #10
    Registered User
    Join Date
    06-08-2005
    Posts
    67

    Re: VLOOKUP searching array in memory

    I make all holiday dates serial, and my process date serial, I should be ok?

  11. #11
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VLOOKUP searching array in memory

    Quote Originally Posted by mwc0914 View Post
    I make all holiday dates serial, and my process date serial, I should be ok?
    Do that and use Application.Match, then it should work.

    EDIT:
    I just tested with a 1-dimensional array. VLookup doesn't work, but HLookup does...

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 04-19-2018 at 07:41 PM.

  12. #12
    Registered User
    Join Date
    06-08-2005
    Posts
    67

    Re: VLOOKUP searching array in memory

    Got it, with process_date defined as Date and did HLookup...thanks!
    Last edited by mwc0914; 04-19-2018 at 08:19 PM.

+ 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. [SOLVED] Thrashing VBA Memory Leaks. Memory Loop Problems. Uncleared Memory in .Match Range Object
    By Doc.AElstein in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-10-2017, 05:17 PM
  2. Memory size of an object like array
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-18-2017, 06:13 AM
  3. [SOLVED] VBA: Vlookup / Match on Array in memory
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-12-2016, 05:06 PM
  4. Replies: 2
    Last Post: 03-03-2014, 05:43 PM
  5. Out of Memory when i make array
    By morpheus305 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-13-2009, 10:27 PM
  6. Request code help, sorting array in memory
    By KR in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2005, 03:45 PM
  7. [SOLVED] Out of Memory: Array Transpose
    By TheVisionThing in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-01-2005, 04:06 PM

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