PDA

View Full Version : Words in a String to Columns



sham_shah
06-15-2007, 11:40 PM
I have a list of strings in Column A. For Instance:
10/06/2007 Company Test1 3.0 2007-08/FAS/569 30/07/2007
16/06/2007 Company Example2 10.0 2007-08/FAS/569 06/07/2007

How do can extract information from this string so that I can have all the required fields in different columns (:10/06/2007"; "Company Test1"; "3.0"; "2007-08/FAS/569"; "30/07/2007"). Right now I use MID() and SEARCH(), but I am unsure how to use these functions in VBA.

Thanks

mikerickson
06-16-2007, 12:21 AM
Would Text to Columns help you?
In VBA, that would look like

Range("A:A").TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1))

mdmackillop
06-16-2007, 12:59 AM
If the ONLY spaces are between the strings
Sub SplitData()
For Each cel In Selection
tmp = Split(cel)
cel.Resize(, UBound(tmp)+1) = tmp
Next
End Sub

mdmackillop
06-16-2007, 01:21 AM
Looking a bit closer, I see you are outputting 5 columns. This will join the Company part into one column, irrespective of spaces in that part of the data.
Sub SplitData2()
Dim tmp, cel As Range, i As Long, j As Long, Company As String
For Each cel In Selection
Company = ""
tmp = Split(cel)
i = UBound(tmp)
cel = DateValue(tmp(0))
cel.Offset(, 2) = tmp(i - 2)
cel.Offset(, 3) = tmp(i - 1)
cel.Offset(, 4) = DateValue(tmp(i))
For j = 1 To i - 3
Company = Company & tmp(j) & " "
Next
cel.Offset(, 1) = Trim(Company)
Next
End Sub