2

I'm currently handling a lot of data with arrays in Excel VBA. Until now source data containing blank cells have been treated as zero, but now I'll have to try and distinguish between blank cells and cells with zeroes in them.

Is it possible to make the array output nothing rather than zero? Such that...

aArray(5) as double
Range("A1:E5") = aArray()

Would only output values in the cells of the range that correspond to array elements that have been filled with something? If the array is empty I'd still get [0,0,0,0,0]

2
  • 6
    You have declared aArray as Double so empty gets converted to zero. You should declare it as Variant. Commented Apr 13, 2012 at 9:13
  • @TonyDallimore you should make that an answer Commented Apr 13, 2012 at 13:03

1 Answer 1

3

Converted from comment as kindly suggested by psubsee2003.

You have declared aArray as Double so empty elements gets converted to zero. You should declare it as Variant.

Sign up to request clarification or add additional context in comments.

3 Comments

I was actually typing this exact answer until I saw your comment. As much as people hate the lack of type safety of Variant, it is the best way to handle some of the features of excel, specifically identifying 0 and blank fields as being different.
Wow, it didn't even cross my mind to use the variant type since I try to avoid it as much as possible. But it's the obvious answer in this situation. Thanks a lot!
You are right to avoid data type Variant; there is a overhead with something that can be an integer then a string then an array and there is no type protection. However, a cell has almost as much flexibility as a variant and it is the only data type to which any cell can be transferred without error or lost. You can use function VarType to tell you what data type a Variant is holding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.