Count text in cell and find next cell with one more

Gimics

Board Regular
Joined
Jan 29, 2014
Messages
164
Office Version
  1. 365
Platform
  1. Windows
Hey team!

I'm working with some hierarchical data output from our ERP system's vanilla reporting software and my finance team wants a low maintenance excel spreadsheet that identifies all of the levels of a hierarchy as attributes / dimensions. Our hierarchies are built as parent/child combos, not as attribute/level rolls ups, so we don't really have a table that's stored with each individual level of the hierarchy for each GL account.

I would love to land on a formula that I can use to identify the levels of the hierarchy for each GL. The report outputs the hierarchy cascading with the hierarchy nodes below each GL account. The nodes are identified with leading asterisk characters (I know, that makes things a little more challenging), that increase in quantity as we get closer to the top of the hierarchy.

Here's an example of what the report spits out:
A
Hierarchy
1GL1
2GL2
3GL3
4***Gross Slaes
5GL4
6GL5
7***Discounts
8****Net Sales

<tbody>
</tbody>


What I am working towards is this:
ABC
HierarchyLevel 1Level 2
1GL1***Gross Sales****Net Sales
2GL2***Gross Sales****Net Sales
3GL3***Gross Sales****Net Sales
4***Gross Sales
5GL4***Discounts****Net Sales
6GL5***Discounts****Net Sales
7***Discounts
8****Net Sales

<tbody>
</tbody>

Level 1 is relatively simple, I just needed to find the next value with an "*", or the parent to the GL accounts. I used a formula like this (note, I'm trimming everything because my actual data has a ton of unneeded spaces):

=IF(LEFT(TRIM($A2),2)="GL",INDEX($A2:$A$8,MATCH("~*",LEFT(TRIM($A2:$A$8),1),0),),"")

Level 2, however, I think I need to find the length of "*"s in a cell, then find the next cell that's 1 "*" longer.

I know I can use a "len() - len(substitute)" formula to find how many "*"s there are, like this:

=LEN(TRIM($B2))-LEN(SUBSTITUTE(TRIM($B2),"*",""))

... and could add "+1" to this to know the number of "*" I need to lookup, but I'm at a loss for what formula to use now to find the next record with +1 "*"s from the value I've found.


I am hoping to figure this out with formulas as the maintenance for the eventual users will be much easier than a macro. The current hierarchy is 1,000 records long and there are 10 different levels in some places, so this would be a very tedious process to replicate manually whenever there are hierarchy updates.

Thanks in advance!!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I would create a helper row. Something like this


A
B
C
1
Hierarchy​
Level 1​
Level 2​
2
Chars​
3​
4​
3
GL1​
***Gross Sales​
****Net Sales​
4
GL2​
***Gross Sales​
****Net Sales​
5
GL3​
***Gross Sales​
****Net Sales​
6
***Gross Sales​
7
GL4​
***Discounts​
****Net Sales​
8
GL5​
***Discounts​
****Net Sales​
9
***Discounts​
10
****Net Sales​

Helper row in gray

Array formula in B3 copied across and down
=IF(LEFT(TRIM($A3),2)="GL",INDEX($A3:$A$100,MATCH(REPT("~*",B$2)&"*",TRIM($A3:$A$100),0),),"")
Ctrl+Shift+Enter

Hope this helps

M.
 
Upvote 0
Thanks Marcelo,

Unfortunately, there are varying levels within the hierarchy for all GL accounts. That is, not all GL accounts belong to the lowest level (ex. Level 1); their first level might actually have 5 "*"s or more.

This is why I was working towards using the number of "*"s in column B as the count.

I honestly didn't know about the =REPT() formula though - that solved my problems, I used it to repeat the number of "*"s based on the number I had found using my formula in the first post. With the full data set, this is what my actual formulas look like:

Parent formula (would be in column "B", where column C in the formula is actually column "A" in my example):

{=IFERROR(IF(ISNUMBER(--LEFT(TRIM($C3),1)),INDEX($C3:$C$757,MATCH("~*",LEFT(TRIM($C3:$C$757),1),0),),INDEX($C3:$C$757,MATCH(REPT("~*",(LEN(TRIM($C3))-LEN(SUBSTITUTE(TRIM($C3),"*","")))+1),LEFT(TRIM($C3:$C$757),(LEN(TRIM($C3))-LEN(SUBSTITUTE(TRIM($C3),"*","")))+1),0),)),"")}


Levels formula (would be in each column following "C", where column C in my formula is actually column "A" in my example, and column "C" in my formula is actually column "B" in my formula):

{=IF(F3="","",IFERROR(INDEX($C3:$C$757,MATCH(REPT("~*",(LEN(TRIM(F3))-LEN(SUBSTITUTE(TRIM(F3),"*","")))+1),LEFT(TRIM($C3:$C$757),(LEN(TRIM(F3))-LEN(SUBSTITUTE(TRIM(F3),"*","")))+1),0),),""))}


Each is an array formula (Ctrl+Shift+Enter) to appropriately evaluate the left(,N) characters, and trim the hierarchy values before evaluating.

This worked!
 
Last edited:
Upvote 0
I'm glad you found a solution. And also for having helped a little with the REPT function :)
Good job :cool:

M.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top