Solved

Text String Manipulation in Excel-VBA

Posted on 2015-01-14
14
370 Views
Last Modified: 2015-01-15
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
Comment
Question by:Mehawitchi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
14 Comments
 
LVL 12

Expert Comment

by:FarWest
ID: 40548944
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
 

Author Comment

by:Mehawitchi
ID: 40549000
Hi fryezz - There is semicolon in the 4 lines
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40549016
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
Technology Partners: 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!

 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40549164
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
 
LVL 81

Accepted Solution

by:
byundt earned 200 total points
ID: 40549181
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
 
LVL 12

Expert Comment

by:FarWest
ID: 40549409
check this sheet, it works without VBA, I hope it will help
0
 
LVL 12

Assisted Solution

by:FarWest
FarWest earned 140 total points
ID: 40549413
sorry this is the file
Book1for.xlsx
0
 
LVL 27

Assisted Solution

by:Glenn Ray
Glenn Ray earned 80 total points
ID: 40549483
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
 
LVL 12

Expert Comment

by:FarWest
ID: 40549514
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
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 80 total points
ID: 40549585
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
 
LVL 81

Expert Comment

by:byundt
ID: 40549830
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
 

Author Comment

by:Mehawitchi
ID: 40550640
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
 

Author Closing Comment

by:Mehawitchi
ID: 40550671
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
 
LVL 45

Expert Comment

by:aikimark
ID: 40551834
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question