1

When I am programming in VBA, I want to initialise an array as

Dim Testarray(10) As String

Now: What are the initial values of this array without further ado?

Or asked in another way: I want to use this Testarray in order to find out certain values. These certain values are then changed to "Missing Line" or whatsoever. By that, I am able to check all the array values later on in order to find the number of my missing values by going through the respective array via, e.g., a for-loop or something like that.

(Bonus question: Right now, I am learning how to write VBA code. Does an array always have to have a name beginning with a capital letter? Or is it also allowed to begin it with a small letter?)

Qralnaq
  • 73
  • 1
  • 7
  • When declaring `Dim Testarray(10) As String` , all 11 elements of Testarray are `""`. Regarding the Bonus, the name doesn’t have to start with Capital letters, even though I personally prefer all my variables to start with capital letters (for debug purposes) – Shai Rado Nov 08 '16 at 12:24

3 Answers3

2

The default value of a String is vbNullString.

This constant evaluates to "" in comparisons, and the difference between "" and vbNullString seems to be very small and pertaining mostly to memory economics on a tiny scale.

Take this example:

Sub test()
    Dim Testarray(10) As String

    Debug.Print Testarray(1) = ""
    Debug.Print Testarray(1) = vbNullString
End Sub

Output:

True
True
Community
  • 1
  • 1
Vegard
  • 3,587
  • 2
  • 22
  • 40
1

You would use the keyword vbNullString to check for empty or uninitialized elements of your newly created string array.

Dim Testarray(10) As String
Testarray(5) = "Test"
For i = 0 To UBound(Testarray)
    If Testarray(i) = vbNullString Then
        ' skip
    Else
        Cells(i + 1, 1).Value = Testarray(i)
    End If

Next

Will print "Test" in row 6 column 1 (A), and skip all uninitialized elements. As others have mentioned, in VBA testing for a blank string ("") will produce the same results in the about code snippet. This does not translate to the rest of the .NET framework however, where strings are a little more complex. For that you would use the String.IsNullOrEmpty function.

bstiffler582
  • 102
  • 11
0

The default value for for a string is the empty string "".

As for the bonus question, short answer - no it doesn't matter. Long answer - vba itself is case insensitive, ie you can define names in whatever case you want and then refer to them in the same or a different case. However, most development environments (including the one in excel) will automatically change all further references to a name to the case in which you defined it.

bobajob
  • 1,192
  • 6
  • 12