Exclude Empty Strings from Array
VBA arrays often end up with empty-string ("") elements, for example from data read off a sheet or from the result of the Split function.
VBA has no built-in function for filtering array elements by a condition, so removing empty strings means writing the loop yourself.
This article builds a function that removes empty strings from an array and explains how to use it and what to watch out for.
An empty string is a string of length 0, written "". A string containing only a space, " ", is not an empty string.
The basic approach
- Prepare an array as large as the original
- Loop over the original and copy only the non-empty elements in order
- Finally, shrink the array to the number of copied elements with
ReDim Preserve
Code example: a function that removes empty strings
Public Function RemoveEmptyStrings(ByVal source As Variant) As Variant
Dim result() As Variant
Dim i As Long
Dim count As Long
' If the array has no elements, return an empty array as-is
If UBound(source) < LBound(source) Then
RemoveEmptyStrings = Array()
Exit Function
End If
ReDim result(0 To UBound(source) - LBound(source))
For i = LBound(source) To UBound(source)
If Len(source(i)) > 0 Then
result(count) = source(i)
count = count + 1
End If
Next i
' If every element was empty, return an empty array
If count = 0 Then
RemoveEmptyStrings = Array()
Exit Function
End If
ReDim Preserve result(0 To count - 1)
RemoveEmptyStrings = result
End Function
Here is an example of calling it.
Public Sub RemoveEmptyStringsSample()
Dim originalArray As Variant
Dim cleanedArray As Variant
Dim i As Long
originalArray = Array("Apple", "", "Banana", "", "Cherry", "Date", "")
cleanedArray = RemoveEmptyStrings(originalArray)
' An empty result has UBound < LBound
If UBound(cleanedArray) < LBound(cleanedArray) Then
Debug.Print "No valid elements"
Exit Sub
End If
For i = LBound(cleanedArray) To UBound(cleanedArray)
Debug.Print cleanedArray(i)
Next i
End Sub
The Immediate window prints Apple, Banana, Cherry, and Date.
Key points in the code
- Test with
Len(source(i)) > 0: This gives the same result assource(i) <> "", but does not raise an error if an element isEmptyorNull(both are removed) - Use
LBound/UBound: The starting index of arrays made with theArrayfunction depends onOption Base, and arrays obtained fromRange.Valueare always 1-based. Do not hard-code the starting index - When every element is empty:
ReDim Preserve result(0 To -1)raises an error, so whencount = 0the function returnsArray(), an empty array - Checking the return value: An empty array can be detected with
UBound(array) < LBound(array)
ReDim Preserve rebuilds the array and copies the data, so calling it every time you add one element slows things down. This example allocates the maximum size up front and shrinks it just once at the end.
Removing whitespace-only elements too
To also remove elements made up only of spaces, trim leading and trailing whitespace with Trim before testing.
If Len(Trim(source(i))) > 0 Then
To also exclude full-width spaces, combine this with something like Replace(source(i), " ", "").
Summary
- VBA has no built-in function to remove empty strings, so copy the elements into a new array with a loop
- Prepare an array of the original size, fill it, and shrink it once with
ReDim Preserveat the end for efficiency - Return
Array()when every element is empty so thatReDim ... To -1does not raise an error - Use
Trimwhen you also want to remove whitespace-only elements