Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 56915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:36:34+00:00 2026-05-10T17:36:34+00:00

As the title states, is there a way to prevent extra elements from showing

  • 0

As the title states, is there a way to prevent extra elements from showing up in VBA dynamic arrays when they are non-zero based?

For example, when using code similar to the following:

While Cells(ndx, 1).Value <> vbNullString     ReDim Preserve data(1 To (UBound(data) + 1))     ndx = ndx + 1 Wend 

You have an extra empty array element at the end of processing. While this can be eliminated with the following:

ReDim Preserve data(1 To (UBound(data) - 1)) 

This doesn’t seem like the best way of resolving this problem.

As such, is there a way to prevent that extra element from being created in the first place? Preferably something that doesn’t require additional logic inside of the loop.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:36:34+00:00Added an answer on May 10, 2026 at 5:36 pm

    So this has turned out to be an annoying little problem as it looks like there really isn’t a way to prevent the issue from coming up. Based upon the answers provided by the other users, I tired the following approaches to solving the problem.

    Using a Collection – While this approach works quite will in situations where you need to read and store data, you can’t use user-defined types with a Collection. Being able to define the item key is useful as you can use it to cross-reference two Collections; however, in VBA there is no way to get the list of keys in the Collection which can be limiting.

    Reading an Excel Range into an Array – Another extremely nice approach, but it seems to work best when you know what the ranges are going to be ahead of time. If you have to figure out the ranges on the fly then you might find yourself in a situation where using a Collection or a smaller ReDim array easier to work with.

    Building the Array on the fly with ReDim Preserve – While this can be a fairly straightforward operation, there are two issues involved with it. One is that ReDim can be an expensive operation as Visual Basic actually creates a new array with the given size, copies the old array, and then deletes the old array (or releases it for the Garbage Collector in Visual Basic .NET). So you would want to minimize the calls to ReDim if you are going to be working with extremely large arrays.

    Additionally, you are likely going to run into a situation similar to the one in the question where you have an extra element at the start of the array or at the end of the array that is empty. The only way around this seems to be to either check to see if you need to resize before doing the operation, or to delete the empty element before returning the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title states: Warning: getimagesize failed to open, etc Is there a way
The title pretty much states it. Is there a way for me to unzip
Like the title states, does anyone out there have a clear way to implement
Page Title Is there a way to dinamically change the pages title at runtime?
As stated in the title, is there a way, using regular expressions, to match
As the title states, we're looking for a way to access a .NET 3.5
Simple as the title states: Can you use only Java commands to take a
As the title states when would you recommend the use of the mediator design
As the title states, how expensive are Python dictionaries to handle? Creation, insertion, updating,
As the title states my Ajax call is actually causing the form to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.