+ Reply to Thread
Results 1 to 8 of 8

WorksheetFunction.Sort Pre-pending a Null Value to an Array

  1. #1
    Registered User
    Join Date
    02-24-2022
    Location
    Ozaukee County, Wisconsin, USA
    MS-Off Ver
    Office 365 and Office 2013
    Posts
    19

    Arrow WorksheetFunction.Sort Pre-pending a Null Value to an Array

    Dear Forum,

    I have this really weird thing happening in my VBA that I've never run across, and I'm wondering if anyone else has? I am populating a variant array with Long values, and then modifying the array by using a WorksheetFunction.Sort to it. Upon doing so, the array gets pre-pended with a null value, as shown below. Has anyone ever heard of this? Here's my code, and the results from the Immediate window.

    Please Login or Register  to view this content.
    The code errors-out later at this line, when I try to get the first value from the sorted array. The array is not read or modified between the Sort and the 'EHLegacyIDs(0)' portion of code.

    Please Login or Register  to view this content.
    The error I get is a 'Subscript out of Range' error.

    Here's the proof that the null value is getting pre-pended, from the Immediate window. These Immediate statements were run when the code was stopped at the error location.

    Please Login or Register  to view this content.
    Also, if I send a
    Please Login or Register  to view this content.
    command to the Immediate window at this point, I get the 'Subscript out of Range' error.

    Has anyone heard of this happening? I'm using Office 365 on a Windows 10 machine.

    Brian

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

    Re: WorksheetFunction.Sort Pre-pending a Null Value to an Array

    I appears that the EHLegacyIDs result array is 1-based (1 to 2650) and not zero-based (0 to 2649).

    What is the lower boundary of the array
    Please Login or Register  to view this content.
    Do you have the line Option Base 1 at the top of your code module?
    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
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: WorksheetFunction.Sort Pre-pending a Null Value to an Array

    From what I've found, using the worksheet function Sort, will always return a 1 based array regardless of your settings.
    Therefore there will never be an element 0, which is why you get the error.

  4. #4
    Registered User
    Join Date
    02-24-2022
    Location
    Ozaukee County, Wisconsin, USA
    MS-Off Ver
    Office 365 and Office 2013
    Posts
    19

    Re: WorksheetFunction.Sort Pre-pending a Null Value to an Array

    Fluff13,

    Well, in my humble opinion, that is just silly for Excel to do that. But that appears to be what is happening. I was able to verify that by checking the Lbound at the error point, as AlphaFrog suggested. I guess, add one "hate" point to the tally of my love/hate relationship with Excel!

    Brian

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: WorksheetFunction.Sort Pre-pending a Null Value to an Array

    that is just silly for Excel to do that
    But you cannot have a zero based array in Excel. If you try to index an array with 0 for the row it will return the entire array.

  6. #6
    Registered User
    Join Date
    02-24-2022
    Location
    Ozaukee County, Wisconsin, USA
    MS-Off Ver
    Office 365 and Office 2013
    Posts
    19

    Re: WorksheetFunction.Sort Pre-pending a Null Value to an Array

    Mmmm, are you sure? I used zero-based arrays in Excel VBA all the time. This was the first time I encountered a problem like this with an array.

    Brian

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: WorksheetFunction.Sort Pre-pending a Null Value to an Array

    You can have 0 based arrays in VBA, but not in xl & as you are using a worksheet function, it will act the same as it would in a sheet.
    VBA & Excel are not the same thing.

  8. #8
    Registered User
    Join Date
    02-24-2022
    Location
    Ozaukee County, Wisconsin, USA
    MS-Off Ver
    Office 365 and Office 2013
    Posts
    19

    Re: WorksheetFunction.Sort Pre-pending a Null Value to an Array

    Ah. I already knew that VBA and Excel are not the same thing, but I was not aware that worksheet functions would operate differently than other VBA functions. Guess every day is a day to learn something new. Thanks!

    Brian

+ 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. Array worksheetFunction.sum
    By olsonkyle12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2021, 05:29 PM
  2. [SOLVED] Worksheetfunction.min not working with array
    By SJGORILLA in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-31-2019, 06:01 AM
  3. [SOLVED] vba for using WorksheetFunction with array formula
    By Andy Swain in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-29-2019, 11:52 AM
  4. Array Formula Using WorksheetFunction
    By tbaker818 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2017, 03:41 AM
  5. WorksheetFunction.Percentile - Array Formula
    By Fjcosta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-03-2015, 02:54 PM
  6. [SOLVED] 'null reference' when trying to use WorksheetFunction from C#
    By RandomEngineer in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-24-2006, 11:25 PM
  7. Array Formula Using WorksheetFunction
    By xcelion in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-18-2005, 07:15 AM

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