Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Text String Manipulation in Excel-VBA

Hello experts,

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.

Below is a sample of the 4 different cases

 Size (C ; % ; A) :  (5374 ; 100% ;  15,412,000.00)
Target: M15-24 Size (C ; % ; A) :  (832 ; 15.55% ;  2,396,566.00)
Filter: Sat;  Size (C ; % ; A) :  (800 ; 14.29% ;  2,202,375.00)
Filter: Sat; Target: M15-24 Size (C ; % ; A) :  (123 ; 2.21% ;  340,605.00)

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.

Best,
Hani
0
Mehawitchi
Asked:
Mehawitchi
  • 5
  • 3
  • 2
  • +2
4 Solutions
 
FarWestCommented:
are you sure there is no semicolon here in the second line
Target: M15-24 ;Size (C ; % ; A) :  (832 ; 15.55% ;  2,396,566.00)
0
 
MehawitchiAuthor Commented:
Hi fryezz - There is semicolon in the 4 lines
0
 
FarWestCommented:
I mean after "Target: M15-24"  and before "Size (C ; % ; A) :  (832 ; 15.55% ;  2,396,566.00)"
in the second line, because it is not shown in your sample
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Glenn RayExcel VBA DeveloperCommented:
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)
0
 
byundtCommented:
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 String
Dim j As Long, k As Long
Sizer = "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 If
End Function

Function Filtr(sInput As String) As String
Dim j As Long, k As Long, k1 As Long, k2 As Long, k3 As Long, k4 As Long
Dim s As String
Filtr = "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 If
End Function

Function Target(sInput As String) As String
Dim j As Long, k As Long, k1 As Long, k2 As Long, k3 As Long, k4 As Long
Dim s As String
Target = "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 If
End Function

Open in new window

The code should be installed in a regular module sheet, as shown in the attached workbook.
SizeFilterTargetQ28596434.xlsm
0
 
FarWestCommented:
check this sheet, it works without VBA, I hope it will help
0
 
FarWestCommented:
sorry this is the file
Book1for.xlsx
0
 
Glenn RayExcel VBA DeveloperCommented:
No Points.  

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.

Here's a revised workbook.
-Glenn
EE-Book1for.xlsx
0
 
FarWestCommented:
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 :)
0
 
aikimarkCommented:
The following regular expression:
(?:Filter: ([^;]+);)?\s*(?:Target: ([^ ]+))?\s*Size .* (\d[^)]*)\)

Open in new window

produces the following parsed results:
Match 0
SubMatch 0: 
SubMatch 1: 
SubMatch 2: 15,412,000.00

Match 1
SubMatch 0: 
SubMatch 1: M15-24
SubMatch 2: 2,396,566.00

Match 2
SubMatch 0: Sat
SubMatch 1: 
SubMatch 2: 2,202,375.00

Match 3
SubMatch 0: Sat
SubMatch 1: M15-24
SubMatch 2: 340,605.00

Open in new window

Where...
SubMatch(0) is the Filter value
SubMatch(1) is the Target value
SubMatch(2) is the Size value
0
 
byundtCommented:
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.

Thanks again for all proposed solutions
0
 
aikimarkCommented:
Here is the code for your regular expression testing.
Option Explicit


Public 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 If
End Function

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now