Need a macro to extract certain values / characters in a cell

  • Thread starter Thread starter Johin Chandresh.B
  • Start date Start date
J

Johin Chandresh.B

Hi Guys,

I have a long sentence in one cell (A1) which contains such information

Q/O - 03T7032 QTY - 1 SYSTEM BOARD (PLANAR) ETA 16/08/2013 @ 09:00AM ORDER # - 112348135 Q/O 043N9877 QTY - 1 65W HsFGDDdEAfT SsdFGeNK FadAFfN ETA 16/08/2013 @ 09:00A M ORDER# - 178123235

I have to extract Q/O - 03T7032 ETA 16/08/2013 ORDER # - 112348135 Q/O 043N9877 ETA 16/08/2013 ORDER# - 178123235 in separate cells

There are about 2000 lines and as of now am doing it manually.

Anyone one has the knowledge of building macros, please help.

Regards,
Joe
 
One way with benefits! It has reusable helper routines...

<In a standard module:>

Option Explicit

Type udtAppModes
Events As Boolean: CalcMode As Long: Display As Boolean: CallerID As
String
End Type
Public AppMode As udtAppModes


Sub ParseString()
Const sSource As String = "ParseString()"
Dim vData, v, v0, v1, v2, n&, j&, s1$

vData = Range(Cells(1, 1), Cells(1, 1).End(xlDown))
On Error GoTo skipit
EnableFastCode sSource
For n = LBound(vData) To UBound(vData)
v0 = Split(vData(n, 1), "QTY"): s1 = ""
For j = LBound(v0) To UBound(v0)

'Get Q/O
v1 = Split(v0(0), " ")
s1 = s1 & "~" & "Q/O " & FilterString(v1(2))
RemoveTrailingSpaces s1
v1 = Split(v0(1), "ETA"): v2 = Split(v1(1), "ORDER")

'Get ETA
s1 = s1 & "~ETA " & DateValue(FilterString(v2(0), "/ :"))
RemoveTrailingSpaces s1

'Get ORDER#
v = Split(v2(1), "Q/O")
s1 = s1 & "~ORDER# " & FilterString(v(1), , False)

skipit:
Next 'j
'Split into adjacent cells in same row
v = Split(Mid(Replace(s1, " ", " "), 2), "~")
Cells(n, 2).Resize(1, UBound(v) + 1) = v
Next 'n
Cells(1, 2).Resize(, ActiveSheet.UsedRange.Columns.Count -
1).EntireColumn.AutoFit
EnableFastCode sSource, False
End Sub

Function RemoveTrailingSpaces$(TextIn$)
Dim s1$, k%
s1 = TextIn
For k = 1 To 2
If Right(s1, 1) = " " Then s1 = Mid(s1, 1, Len(s1) - 1)
Next 'j
RemoveTrailingSpaces = Replace(s1, " ", " ")
End Function

Function FilterString$(ByVal TextIn As String, _
Optional IncludeChars As String, _
Optional IncludeLetters As Boolean = True, _
Optional IncludeNumbers As Boolean = True)
' Filters out all unwanted characters in a string.
' Arguments: TextIn The string being filtered.
' IncludeChars [Optional] Keeps any characters.
' IncludeLetters [Optional] Keeps any letters.
' IncludeNumbers [Optional] Keeps any numbers.
'
' Returns: String containing only the wanted characters.

Const sSource As String = "FilterString()"

'The basic characters to always keep
Const sLetters As String = "abcdefghijklmnopqrstuvwxyz"
Const sNumbers As String = "0123456789"

Dim i As Long, CharsToKeep As String

CharsToKeep = IncludeChars
If IncludeLetters Then _
CharsToKeep = CharsToKeep & sLetters & UCase(sLetters)
If IncludeNumbers Then CharsToKeep = CharsToKeep & sNumbers

For i = 1 To Len(TextIn)
If InStr(CharsToKeep, Mid$(TextIn, i, 1)) Then _
FilterString = FilterString & Mid$(TextIn, i, 1)
Next
End Function 'FilterString()

Sub EnableFastCode(Caller$, Optional SetFast As Boolean = True)
'The following will make sure only the Caller has control,
'and allows any Caller to take control when not in use.
If AppMode.CallerID <> Caller Then _
If AppMode.CallerID <> "" Then Exit Sub

With Application
If SetFast Then
AppMode.Display = .ScreenUpdating
.ScreenUpdating = False
AppMode.CalcMode = .Calculation
.Calculation = xlCalculationManual
AppMode.Events = .EnableEvents: .EnableEvents = False
AppMode.CallerID = Caller
Else
.ScreenUpdating = AppMode.Display
.Calculation = AppMode.CalcMode
.EnableEvents = AppMode.Events
AppMode.CallerID = ""
End If
End With
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
For example, you show Q/0 and also Q/O (zero and capital "O")
You also show ORDER and Order.

Good point on the case issue, though I didn't get any Q/0 in the
example strings I copy/pasted. My suggestion could be modified as
follows to address the case issue...

change this

For n = LBound(vData) To UBound(vData)
v0 = Split(vData(n, 1), "QTY"): s1 = ""

to this

For n = LBound(vData) To UBound(vData)
vData(n, 1) = UCase(vData(n, 1))
v0 = Split(vData(n, 1), "QTY"): s1 = ""

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Back
Top