I need to extract some demographic information from Excel sheets using VBA.

The information I need to extract is about "Target" definition and respective "Size" and the statement from which I will be extracting is written in 4 different ways.

Case # 1
Size (C ; % ; A) : (5374 ; 100% ; 15,412,000.00)

There is no mention of "Target" nor "Filter" in the text, and the text starts with the word " Size". The output should be:

Target:
Filter:
Size: 15,412,000.00

-------------------------------------------

Case # 2
Target: M15-24 Size (C ; % ; A) : (832 ; 15.55% ; 2,396,566.00)

There is no mention of "Filter" in the text, but there is mention of "Target" and the text starts with the word "Target". The output should be:

Target: M15-24
Filter:
Size: 2,396,566.00

-------------------------------------------

Case # 3
Filter: Sat; Size (C ; % ; A) : (800 ; 14.29% ; 2,202,375.00)

There is no mention of "Target" in the text, but there is mention of "Filter" and the text starts with the word "Filter". The output should be:

Target:
Filter: Sat
Size: 2,202,375.00

-------------------------------------------

Case # 4
Filter: Sat; Target: M15-24 Size (C ; % ; A) : (123 ; 2.21% ; 340,605.00)

There is mention of "Filter" and "Target" in the text. The output should be:

Target: M15-24
Filter: Sat
Size: 340,605.00

--------------------------------------------

Please note the following:
A) The output format is constant. If either the "Target" or "Filter" information is available in the text, it will be included in the output, otherwise it will simply be left blank
B) The size is always included in the text, and it is always the last part of the text, so it can be extracted using Excel/VBA advanced text formulas
C) The first line in the VBA macro will be: Select "A1" because this is where the above statement will be written

I hope I have well explained what I'm trying to do. Please let me know if you need any further clarification.

In fact, there is no semi-colon delimiter between the "Target" and "Size" clauses in either Case #2 or Case #4. Is that the norm? That is, if a Target and Size exist, is there only a space separating them?

Also, is there ever a condition where the Size does not exist? (Hopefully not)

I parsed your data using semi-colon, space and Size, Filter or Target as delimiters with the following user-defined functions. They may be used with worksheet formulas like:
=Sizer(A1)
=Filtr(A1)
=Target(A1)

Function Sizer(s As String) As StringDim j As Long, k As LongSizer = "Size: "j = InStr(1, s, "Size", vbTextCompare)If j > 0 Then k = InStrRev(s, " ") Sizer = Sizer & Mid(Left(s, Len(s) - 1), k + 1)End IfEnd FunctionFunction Filtr(sInput As String) As StringDim j As Long, k As Long, k1 As Long, k2 As Long, k3 As Long, k4 As LongDim s As StringFiltr = "Filter: "j = InStr(1, sInput, "Filter:", vbTextCompare)If j > 0 Then s = Trim(Mid(sInput, j + 7)) k1 = InStr(1, s, " ", vbTextCompare) k2 = InStr(1, s, ";", vbTextCompare) k3 = InStr(1, s, "Target:", vbTextCompare) k4 = InStr(1, s, "Size", vbTextCompare) k = Len(s) k = IIf(k1 > 0, Application.Min(k, k1), k) k = IIf(k2 > 0, Application.Min(k, k2), k) k = IIf(k3 > 0, Application.Min(k, k3), k) k = IIf(k4 > 0, Application.Min(k, k4), k) Filtr = Filtr & Trim(Left(s, k - 1))End IfEnd FunctionFunction Target(sInput As String) As StringDim j As Long, k As Long, k1 As Long, k2 As Long, k3 As Long, k4 As LongDim s As StringTarget = "Target: "j = InStr(1, sInput, "Target:", vbTextCompare)If j > 0 Then s = Trim(Mid(sInput, j + 7)) k1 = InStr(1, s, " ", vbTextCompare) k2 = InStr(1, s, ";", vbTextCompare) k3 = InStr(1, s, "Filter:", vbTextCompare) k4 = InStr(1, s, "Size", vbTextCompare) k = Len(s) k = IIf(k1 > 0, Application.Min(k, k1), k) k = IIf(k2 > 0, Application.Min(k, k2), k) k = IIf(k3 > 0, Application.Min(k, k3), k) k = IIf(k4 > 0, Application.Min(k, k4), k) Target = Target & Trim(Left(s, k - 1))End IfEnd Function

I like fryezz's non-VBA solution and have consolidated the formulas to eliminate helper columns. This makes the formulas a little more complicated-looking. The only change I made was to use the VALUE() function on the Size result.

Thanks A lot Glenn,
25 years ago or so I practiced using helping column not only for simplifying formulas but also to gain performance, now we have powerful machines, but still in my blood :)

If you want a formula solution, here are some to consider (for Target, Filter & Size). They are much shorter than the ones previously proposed--but not really any easier to understand.
=IF(COUNTIF(A2,"*Target*"),MID(LEFT(A2,SEARCH("Size",A2)-2),SEARCH("Target",A2)+8,99),"") =IF(COUNTIF(A2,"Filter*"),MID(LEFT(A2,FIND(";",A2,9)-1),9,99),"")
=TRIM(MID(LEFT(A2,LEN(A2)-1),FIND("|",SUBSTITUTE(A2,";","|",LEN(A2)-LEN(SUBSTITUTE(A2,";",""))))+1,99))

These formulas assume that the data matches the sample given. I show them in action in columns E:G of Glenn's workbook. EE-Book1forQ28596434.xlsx

0

MehawitchiAuthor Commented:

Thank you so much fryezz, Glenn Ray, aikimark and byundt for your great contributions.

I am currently testing your proposed solutions and will get back to you shortly with my comments.

Thanks again,
Hani

0

MehawitchiAuthor Commented:

After going through all the proposed solutions, I think will go for the one proposed by byundt because it can easily be incorporated in my VBA macro.

Here is the code for your regular expression testing.

Option ExplicitPublic Function Q_28596434(ByVal parmString As String) As String Static oRE As Object Dim oMatches As Object Dim oM As Object If oRE Is Nothing Then Set oRE = CreateObject("vbscript.regexp") oRE.Pattern = "(?:Filter: ([^;]+);)?\s*(?:Target: ([^ ]+))?\s*Size .* (\d[^)]*)\)" End If If oRE.test(parmString) Then Set oMatches = oRE.Execute(parmString) Set oM = oMatches(0) With oM Q_28596434 = "Target: " & .submatches(1) & vbCrLf Q_28596434 = Q_28596434 & "Filter: " & .submatches(0) & vbCrLf Q_28596434 = Q_28596434 & "Size: " & .submatches(2) End With Else Q_28596434 = vbNullString End IfEnd Function

Target: M15-24

Size (C ; % ; A) : (832 ; 15.55% ; 2,396,566.00);