Problem with Split Function

Neerja

Registered User.
Local time
Today, 14:48
Joined
May 22, 2006
Messages
11
I am trying to upload some data from a .csv file to access table.
For this I am using Input Line and then using the split function with “,” as delimiter to put the values in an array and then upload it to access.

The file that I want to upload, has a number column as amount. It has no $ sign and is listed in the file as say 5,0750,99.03.

When I try to split it, it splits the amount as welll giving me 5 and 0750 amd 99.03 as separate array item.

Any suggestions please??

Thanks in advance
 
Extract comma delimited numbers from Split .csv record

A little harder than I thought, but here it is...
Code:
Sub TestCondense()
   Dim var
   Dim i As Integer
   var = Split("hello,1,200,234.56,1,250,000,middle,1,000.00,goodbye", ",")
   var = Condense(var)
   
   For i = 0 To UBound(var)
      Debug.Print var(i)
   Next i
   Debug.Print
   
End Sub

Code:
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

Cheers,
 
split Function

Thanks Logbolt,

I think this should work.., will try the it out with my file and get back to you..!!

Thanks again..!!

Neerja
 
Problem with split function

Thanks Lagbolt,

The code worked ..

Thanks a lot..!!

Neer
 

Users who are viewing this thread

Back
Top Bottom