title: Declare a 0-Length String Array in VBA - Impossible?
body: <p>Is it really not possible to declare a 0-length array in VBA? If I try this:</p>
  
  <pre><code>Dim lStringArr(-1) As String
  </code></pre>
  
  <p>I get a compile error saying range has no values. If I try to trick the compiler and redim at runtime like this:</p>
  
  <pre><code>ReDim lStringArr(-1)
  </code></pre>
  
  <p>I get a subscript out of range error.</p>
  
  <p>I've varied the above around a bit but with no luck e.g.</p>
  
  <pre><code>Dim lStringArr(0 To -1) As String
  </code></pre>
  
  <h1>Use Case</h1>
  
  <p>I want to convert a variant array to a string array. The variant array may be empty as it comes from the Keys property of a dictionary. The keys property gives back an array of variants. I want an array of strings to use in my code, as I have some functions for processing string arrays I'd like to use. Here's the conversion function I'm using. This throws a subscript out of range error due to lMaxIndex being = -1:</p>
  
  <pre><code>Public Function mVariantArrayToStringArray(pVariants() As Variant) As String()
      Dim lStringArr() As String
      Dim lMaxIndex As Long, lMinIndex As Long
      lMaxIndex = UBound(pVariants)
      lMinIndex = LBound(pVariants)
      ReDim lStringArr(lMaxIndex)
      Dim lVal As Variant
      Dim lIndex As Long
      For lIndex = lMinIndex To lMaxIndex
          lStringArr(lIndex) = pVariants(lIndex)
      Next
      mVariantArrayToStringArray = lStringArr
  End Function
  </code></pre>
  
  <h2>Hack</h2>
  
  <p>Return a singleton array containing an empty string. Note- this isn't what we want. We want an empty array- such that looping over it is like doing nothing. But a singleton array containing an empty string will often work e.g. if we later want to join all the strings together in the string array.</p>
  
  <pre><code>Public Function mVariantArrayToStringArray(pVariants() As Variant) As String()
      Dim lStringArr() As String
      Dim lMaxIndex As Long, lMinIndex As Long
      lMaxIndex = UBound(pVariants)
      lMinIndex = LBound(pVariants)
      If lMaxIndex &lt; 0 Then
          ReDim lStringArr(1)
          lStringArr(1) = ""
      Else
          ReDim lStringArr(lMaxIndex)
      End If
      Dim lVal As Variant
      Dim lIndex As Long
      For lIndex = lMinIndex To lMaxIndex
          lStringArr(lIndex) = pVariants(lIndex)
      Next
      mVariantArrayToStringArray = lStringArr
  End Function
  </code></pre>
  
  <h2>Update since answer</h2>
  
  <p>Here is the function I'm using for converting a variant array to a string array. Comintern's solution seems more advanced and general, and I may switch to that one day if I'm still stuck coding in VBA:</p>
  
  <pre><code>Public Function mVariantArrayToStringArray(pVariants() As Variant) As String()
      Dim lStringArr() As String
      Dim lMaxIndex As Long, lMinIndex As Long
      lMaxIndex = UBound(pVariants)
      lMinIndex = LBound(pVariants)
      If lMaxIndex &lt; 0 Then
          mVariantArrayToStringArray = Split(vbNullString)
      Else
          ReDim lStringArr(lMaxIndex)
      End If
      Dim lVal As Variant
      Dim lIndex As Long
      For lIndex = lMinIndex To lMaxIndex
          lStringArr(lIndex) = pVariants(lIndex)
      Next
      mVariantArrayToStringArray = lStringArr
  End Function
  </code></pre>
  
  <h1>Notes</h1>
  
  <ul>
  <li>I use Option Explicit. This can't change as it safeguards the rest of the code in the module.</li>
  </ul>