I have a spreadsheet that uses a string convention to convey levels of detail about individual "cases."
The convention is as follows: There are 5 main categories for the data in the string. 1=CATEGORY, 2=TOOLS, 3=DOCUMENTATION, 4=PROCESS, 5=JOB AID.
Each category can have multple sub categories broken down by 1a, 1b, 1c or 2a, 2b, 2c, etc for each 5 category.
Main category is separated from sub category by ":" and sub catory is separated from sub cateogry by "," whereas sub catory is separated from a new Main category by a ";"
an example of a string: "1:1i;2:2a;3:3a,3d,3l;4:4a"
Here is a sample breakdown of category in values:
CATEGORY 1
Incorrect:VG:QOC 1i
TOOLS 2
Macro:Used 2a
DOCUMENTATION 3
TAT:Missing 3a
ROUTING:Missing 3d
STORY:Missing Impact to Health 3i
PROCESS 4
CNX Checklist Not Used 4a
I want to be able to pull out in text form what main categories and sub categories were flagged.
Example query:
Of all strings, how many times was main category 4 flagged?
Of all 4 flagged, how many times was 4a flagged?
What were all elements flagged on one "case" or string?
I can build the strings, I just cannot parse them. Please help... :)
The function you are looking for is Split (this link is for the VB function, but the behavior is virtually the same in VBA). You can pass a specific string to it and specify the delimiter, and it will return an array of each value.
In your case, since your string has multiple delimiters, you'll need to do it multiple times.
First potential issue is what if you do not have a subcategory for a given category? If you will always have at least subcategory for each category in your string, then that's fine, but if there is potentially a case where there is no subcategory, then you need to make sure your highest level grouping is still delimited by a
;
.Since you didn't say how the information is going to get presented to the user, the below example will print something close to what it sounds like you are expecting in the intermediate window in Excel.
This is a function that will count the subcategories easily
And using your example string as an input, the above code will print:
The above code will print every flag, even if there are duplicates. You didn't say whether or not that was desired behavior. Filtering or grouping duplicates from an array is not simple, but is best done with the Collection or Dictionary class in VBA. (Take a look at this question for help filtering duplicates from an array).
The above code is just an example to show what needs done and how to accomplish the parsing (as that was your specific request). To actually make this into working code, you would need to do 2 things:
SplitExample()
above) and give it a name (likeParseErrorCodes
), and have it accept a string parameter calledinputString
. You would then call it from the method that builds the string (which you said you already can do) and pass that string to the method.Debug.Print
lines with something that will output the results somewhere (presumably to another excel spreadsheet so you can create the chart that you were looking for).The basic idea would be: