Solved

Text String Manipulation in Excel-VBA

Posted on 2015-01-14
14
345 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
  • 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
 
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 80

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 80

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now