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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 280k
  • Answers 280k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want your view just to show an image… May 13, 2026 at 3:40 pm
  • Editorial Team
    Editorial Team added an answer Not automatically. You would have to catch scroll events and… May 13, 2026 at 3:40 pm
  • Editorial Team
    Editorial Team added an answer This example uses the bitwise shift operator: "<<". This operator… May 13, 2026 at 3:40 pm

Related Questions

The title is horrible, i know; I'm terrible at titles on SO here. I'm
Here's my table: <table class=detailTable> <tr> <th> Event Title: </th> <td> <asp:Label ID=lblTitle runat=server></asp:Label>
I have an application called Location. Location has Country, State, City, Big_City_Nearby, Longitude, latitude.
So the teacher has posed this assignment: You have been hired by the United

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.