Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to get the number of elements after using Split?

Status
Not open for further replies.

bigracefan

Programmer
Joined
Apr 2, 2002
Messages
304
Location
US
I'm currently counting the comas + 1 to figure out the number of elements per line. The I have a series of "if" statements to handle the output. Is there a way to do this dynamically?
What I'm currenly using:

NumElements = CountCommas(Tmp$)
MyString = Split(Tmp$, ",")
If Len(Tmp$) = 0 Then 'make sure that a Tmp$ has an element or not (will be NumElements = 0)
NumElements = 999
End If
'make sure that the proper number of MyString are in place with each line
If NumElements < 100 And NumElements >= 47 Then
'test results
strValue(i, 0) = Trim(MyString(1)) 'Step Number
strValue(i, 1) = Trim(MyString(2)) 'Name
strValue(i, 2) = Trim(MyString(5)) 'Judge
strValue(i, 3) = Trim(MyString(8)) 'Range
strValue(i, 4) = Trim(MyString(10)) 'Act-value
strValue(i, 5) = Trim(MyString(11)) 'REF-VALUE
'Numbers below need to be converted from scientific notation
If IsNumeric(Trim(MyString(12))) Then
strValue(i, 6) = CDec(Trim(MyString(12))) 'MEAS-VALUE
Else
strValue(i, 6) = Trim(MyString(12)) 'MEAS-VALUE
End If
If InStr(Trim(MyString(13)), "%") > 0 Then
strValue(i, 7) = CDec(strValue(i, 5)) * (1 + (1 * Mid(MyString(13), 1, InStr(1, MyString(13), "%") - 1)) / 100)
Else
If IsNumeric(Trim(MyString(13))) Then
strValue(i, 7) = CDec(Trim(MyString(13))) 'HI-LIMIT
Else
strValue(i, 7) = Trim(MyString(13)) 'HI-LIMIT
End If
End If
If InStr(Trim(MyString(14)), "%") > 0 Then
strValue(i, 8) = CDec(strValue(i, 5)) * (1 + (1 * (Mid(MyString(14), 1, InStr(1, MyString(14), "%") - 1)) / 100))
Else
If IsNumeric(Trim(MyString(14))) Then
strValue(i, 8) = CDec(Trim(MyString(14))) 'LO-LIMIT
Else
strValue(i, 8) = Trim(MyString(14)) 'LO-LIMIT
End If
End If
ElseIf NumElements < 10 And NumElements >= 5 Then
strValue(i, 0) = Trim(MyString(0))
strValue(i, 1) = Trim(MyString(1))
strValue(i, 2) = Trim(MyString(2))
strValue(i, 3) = Trim(MyString(3))
strValue(i, 4) = Trim(MyString(4))
strValue(i, 5) = Trim(MyString(5))
ElseIf NumElements = 4 Then
strValue(i, 0) = Trim(MyString(0))
strValue(i, 1) = Trim(MyString(1))
strValue(i, 2) = Trim(MyString(2))
strValue(i, 3) = Trim(MyString(3))
strValue(i, 4) = Trim(MyString(4))
ElseIf NumElements = 3 Then
strValue(i, 0) = Trim(MyString(0))
strValue(i, 1) = Trim(MyString(1))
strValue(i, 2) = Trim(MyString(2))
strValue(i, 3) = Trim(MyString(3))
ElseIf NumElements = 2 Then
strValue(i, 0) = Trim(MyString(0))
strValue(i, 1) = Trim(MyString(1))
strValue(i, 2) = Trim(MyString(2))
ElseIf NumElements = 1 Then
strValue(i, 0) = Trim(MyString(0))
strValue(i, 1) = Trim(MyString(1))
ElseIf NumElements = 0 Then
If Trim(MyString(0)) <> "HIOKI>" Then
strValue(i, 0) = Trim(MyString(0))
End If
End If
If NumElements <> 999 Then
i = i + 1
End If
Raw$ = Right$(Raw$, Len(Raw$) - (Len(Tmp$) + 1))
Loop
 
Wont you just be using a for...next loop to handle this stuff?

Code:
MyString = Split(Tmp$, ",")
y = Ubound(MyString) + 1 
for x = 0 to y
 'do the first five fields if present
 if x <= 5
     strValue(i, x)  = Trim(MyString(x))
 end if
 
 select case x
 case 12
   'handle MEAS-VALUE
 case 14
   'handle LO-LIMIT

  'etc
  'etc
 case else
   'handle or ignore everything else
 end select

next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top