Split a string

Asked

Viewed 1,041 times

3

I’m trying to make a simple code using the split, but some mistakes are occurring that I’m not able to solve.. no if with asterisk VBA informs me that the subscript this out of range..

Sub Getinstrument()
Dim instrument As String
Dim splitinstrument() As String
Dim i As Integer
Dim removespax As Integer
Dim tam As Integer
removespax = -1
'Set instrument = Range(ActiveCell.Row, B)

instrument = Range("E3")
splitinstrument() = Split(instrument)
tam = Len(instrument) - 1
  For i = 0 To tam
**If splitinstrument(i) <> "" And "x" Then
    removespax = removespax + 1
    splitinstrument(removespax) = splitinstrument(i)
End If
Next
ReDim Preserve splitinstrument(removespax)
MsgBox splitinstrument()
End Sub
  • Really weird code... You should look at the size of the array itself to go through it; instead, you are orienting yourself by the size of the range instrument. You need to debug and identify the content of splitinstrument. This array may be empty.

  • i made the following modifications to the code

  • eu fiz as seguintes modificacoes no codigo instrument = Range("E3")&#xA;splitinstrument() = Split(instrument)&#xA; &#xA;For i = LBound(splitinstrument) To UBound(splitinstrument)&#xA; If splitinstrument(i) <> "" Then&#xA; removespax = removespax + 1&#xA; splitinstrument(removespax) = splitinstrument(i) End If Next I will debug the code and VBA keeps giving error in splitinstrument type 'type Mismatch'. I tried to use the mid function to separate my string but continued with the same problem.

  • Please update your question with the new code and inform the line that the error occurs.

1 answer

3

In Excel, the arrays start at position 1. In the code snippet below, variable i starts with 0, and when evaluating splitinstrument(0) error occurs.

tam = Len(instrument) - 1
For i = 0 To tam
If splitinstrument(i) <> "" And "x" Then

I am not on a computer with Excel to be able to run and check my response, but the message of Subscript out of range indicates this type of problem.

The solution: change the indexes as below:

tam = Len(instrument)   ' Sem decrementar
For i = 1 To tam        ' De 1 a tam , em vez de 0 a tam-1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.