String array help - New to VBA


N

Neal Ostrander

I am trying to read the individual characters of a textbox into the elements
of an array. The code I have is below. I keep getting an error on the line
that calls the mid() function that says the function can't be called in this
manner. I am just trying to learn VBA and would appreciate any help I could
get.

for example if the user enters "value" into the textbox then the elements of
the array should be:
sArray(0) = v
sArray(1) = a
sArray(2) = l
sArray(3) = u
sArray(4) = e

thanks in advance for any help
Neal

Dim iCounter As Integer
Dim sInput As String
Dim sArray() As String

sInput = txtInput.Value

ReDim sArray(Len(txtInput.Value))

For iCounter = 0 To UBound(sArray)

sArray(iCounter) = Mid(sInput, iCounter, 1)

Next iCounter
 
Ad

Advertisements

D

Dirk Goldgar

Neal Ostrander said:
I am trying to read the individual characters of a textbox into the
elements
of an array. The code I have is below. I keep getting an error on the line
that calls the mid() function that says the function can't be called in
this
manner. I am just trying to learn VBA and would appreciate any help I
could
get.

for example if the user enters "value" into the textbox then the elements
of
the array should be:
sArray(0) = v
sArray(1) = a
sArray(2) = l
sArray(3) = u
sArray(4) = e

thanks in advance for any help
Neal

Dim iCounter As Integer
Dim sInput As String
Dim sArray() As String

sInput = txtInput.Value

ReDim sArray(Len(txtInput.Value))

For iCounter = 0 To UBound(sArray)

sArray(iCounter) = Mid(sInput, iCounter, 1)

Next iCounter


As declared, your array has a lower bound of 0, but the first character in
the string is at index position 1. Mid(<string>, 0, 1) is not a valid
function call. Try this:

'----- start of suggested code -----
Dim iCounter As Integer
Dim sInput As String
Dim sArray() As String

sInput = Me.txtInput.Value & vbNullString

If Len(sInput) = 0 Then

' Do something if the text box was empty ...

Else

ReDim sArray(1 To Len(sInput)

For iCounter = 1 To UBound(sArray)
sArray(iCounter) = Mid(sInput, iCounter, 1)
Next iCounter

End If
'----- end of suggested code -----
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top