0

I am wondering how to check if a string array at a certain index has not been initialized.

I am using the Split function to determine the file extension of a file path, but in some cases the extension will not be included. My delimiter is ".", so only in the case that the extension is provided will there be a value at array index 1.

I have tried comparing my array at index 1 using Is Nothing, "", IsEmpty, and vbNullString without any success.

I also tried explicitly declaring the size of the array but then I get issues with assigning it to the value of the Split function.

A snippet of my code is as follows:

Dim filePath As String
Dim stringArr() As String

filePath = "C:\Users\Sarchwalk\Documents\fileName"
stringArr() = Split(filePath, ".") 'Problem here if stringArr size has been declared

If stringArr(1) = "" Then 'Error arises here
    MsgBox "String array at index 1 is empty"
End If
1
  • Try "If UBound(stringArr) = 0 Then" instead of "If stringArr(1) = """ Commented Jun 9, 2017 at 15:09

1 Answer 1

1

Use Ubound,

If Ubound(stringArr) = Lbound(stringArr) Then 
    MsgBox "String array at index 1 is empty"
ElseIf stringArr(1) = "" Then 'Error arises here
    MsgBox "String array at index 1 is empty"
End If

If there is only one then Ubound will equal Lbound. if not then we check for an empty string also.

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

1 Comment

Works great! Thanks :)

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.