Function Condense(varSplit As Variant) As Variant
[COLOR="Green"]' This function extracts information from a comma delimited record of a .csv file
' where the record may contain comma delimited numbers like "1,250,000.00"
' PARAMS: varSplit is a variant array created by "Split"ting the .csv record with a "," delimiter
' RETURN: a variant array with the numbers condensed, and fewer elements than varSplit
' FAILS: when two numbers appear in sequence and the first is not terminated with a "."
' eg. there is not enough information to affect a correct split with "1,1,000" where the first
' number is "1" and the second is "1,000" This algorith requires "1.00,1,000"[/COLOR]
Dim i As Integer
Dim iNumAt As Integer
Dim iShift As Integer
Dim str As String
'indicates postion at which a number is being constructed
'or any number that exceeds array bounds when not building
iNumAt = 999
'traverse array
For i = 0 To UBound(varSplit)
'if current element is numeric
If IsNumeric(varSplit(i)) Then
'if currently building a number
If iNumAt < i Then
'then this is a following number, so condense
varSplit(iNumAt) = varSplit(iNumAt) & Trim(varSplit(i))
'and increment the shift
iShift = iShift + 1
'finally, if the current element contains a "." then stop building
'by setting iNumAt in excess of array bounds
If InStr(1, varSplit(i), ".") > 0 Then iNumAt = 999
Else
'this is the first number in what might be a build
varSplit(i - iShift) = Trim(varSplit(i))
'so set iNumAt to this location
iNumAt = i - iShift
End If
Else
'this item is not numeric, but might need to be shifted
varSplit(i - iShift) = Trim(varSplit(i))
'and end any number builds in progress
iNumAt = 999
End If
Next i
'clear the remaining array members which might have been stranded by shifts
For i = i - iShift To UBound(varSplit)
varSplit(i) = ""
Next i
'rejoin the array as space delimited
str = Join(varSplit, " ")
'dispose of trailing elements, if any
str = Trim(str)
'resplit the string as comma delimited and assign to function
Condense = Split(str, " ")
End Function