Split a string

3

I am trying to make a simple code using split , but there are some errors that I am not able to solve .. in% with% with asterisk VBA informs me that if is 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
    
asked by anonymous 10.09.2014 / 18:02

1 answer

3

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

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

I'm not on an Excel computer to run and check my answer, but the Subscript out of range message indicates this kind 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
    
29.09.2014 / 13:31