Home » excel » excel – vba set array value to NaN

excel – vba set array value to NaN

Posted by: admin May 14, 2020 Leave a comment


I work with double array which are partially filled with data and then printed to a range in a n excel sheet.

The problem is, that all values are by default 0. I would want them to be NaN (not a number), which is represented in the cells as NV(). The double array should convert to NV() automatically if possible.

How to&Answers:

Instead of declaring your Array as double just maintain it as variant

Then you can initialize the Array with #NA using CVErr(xlErrNA)

Sub MyInitializer()
Dim Arr(10)

'Initialize all values to #NA
For i = 0 To UBound(Arr)
Arr(i) = CVErr(xlErrNA)

'Do something with Arr values
Arr(1) = 2.5

Dim Destination As Range
Set Destination = Range("K1")
Set Destination = Destination.Resize(UBound(Arr), 1)
Destination.Value = Application.Transpose(Arr)

End Sub