Populate cells based on criteria

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
679
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I'm looking for a solution to the following if possible:
Is there a formula I can put into Column G (from G4 down) that will return what you can see in the picture? The formula is populating based on the criteria in Range C4:D8 and putting a blank cell between each animal type. A single formula would be great but I can work with intermediate helper cells/formula if necessary. I can work with a VBA solution if necessary but would like to see if there's a formula solution first (any VBA solution can't add rows).
Any help much appreciated.
Capture4.JPG
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
If I read correctly, you have data where you wish to add/delete rows on the go? If so, maybe for Excel2016 a PowerQuery solution could be a nice solution?

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table.Repeat(Table.FromRecords({[Col1=1]}), [Column2])),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Col1"}, {"Custom.Col1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Column2", "Custom.Col1"})
in
    #"Removed Columns"

lMGzA.png
 
Upvote 0
if text split and text join are in 2016, you can use this, it is not clean as it has some blank rows at the end.


mr excel questions 17.xlsm
ABCDEFGH
1
2
3
4Swan3Swan
5Goat5Swan
6Duck2Swan
7Dog1
8Cat3Goat
9Goat
10Goat
11Goat
12Goat
13
14Duck
15Duck
16
17Dog
18
19Cat
20Cat
21Cat
22
23
cjcass
Cell Formulas
RangeFormula
G4:G22G4=TEXTSPLIT(TEXTJOIN(",",,SUBSTITUTE(REPT(C4:C8,D4:D8)&",",RIGHT(C4:C8,1)&LEFT(C4:C8,1),RIGHT(C4:C8,1)&","&LEFT(C4:C8,1))),,",")
Dynamic array formulas.
 
Upvote 0
if text split and text join are in 2016, you can use this, it is not clean as it has some blank rows at the end.


mr excel questions 17.xlsm
ABCDEFGH
1
2
3
4Swan3Swan
5Goat5Swan
6Duck2Swan
7Dog1
8Cat3Goat
9Goat
10Goat
11Goat
12Goat
13
14Duck
15Duck
16
17Dog
18
19Cat
20Cat
21Cat
22
23
cjcass
Cell Formulas
RangeFormula
G4:G22G4=TEXTSPLIT(TEXTJOIN(",",,SUBSTITUTE(REPT(C4:C8,D4:D8)&",",RIGHT(C4:C8,1)&LEFT(C4:C8,1),RIGHT(C4:C8,1)&","&LEFT(C4:C8,1))),,",")
Dynamic array formulas.
Hi,
I entered your formula into G4 and it returns #NAME?
 
Upvote 0
I've just read/seen the intermediate whitespaces. Therefor the PQ solution would become a little longer:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each Table.Repeat(Table.FromRecords({[Col1=1]}), [Column2])),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Col1"}, {"Custom.Col1"}),
    #"Appended Query" = Table.Combine({#"Expanded Custom", #"Added Index"}),
    #"Added Custom1" = Table.AddColumn(#"Appended Query", "Custom", each if [Custom.Col1] = 1 then[Column1] else null),
    #"Sorted Rows" = Table.Sort(#"Added Custom1",{{"Index", Order.Ascending}, {"Custom", Order.Descending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Column1", "Column2", "Index", "Custom.Col1"}),
    #"Removed LastRow" = Table.RemoveLastN(#"Removed Columns", 1)
in
    #"Removed LastRow"

Zceg2.png
 
Upvote 0
If I read correctly, you have data where you wish to add/delete rows on the go? If so, maybe for Excel2016 a PowerQuery solution could be a nice solution?

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Table.Repeat(Table.FromRecords({[Col1=1]}), [Column2])),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Col1"}, {"Custom.Col1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Column2", "Custom.Col1"})
in
    #"Removed Columns"

lMGzA.png
Hi, I'm not wishing to add or delete any rows / columns in the solution and I'm also not familiar with or using PowerBI. I notice your solution hasn't left a gap between the animals. Maybe a standard VBA or formula solution?
 
Upvote 0
Hi, I'm not wishing to add or delete any rows / columns in the solution and I'm also not familiar with or using PowerBI. I notice your solution hasn't left a gap between the animals. Maybe a standard VBA or formula solution?
Yeah, just before your post I added an updated answer to include blank rows. You don't need to be very familiar with PowerQuery to be honest. If you click on either one of your animals you can load your little table into PQ using 'From Table/Range' option in the 'Get & Transform Data' group sitting on the 'Data' tab. Confirm you don't use headers (as per sample). In the screen that opens you'd just paste the above M-code in the 'Advanced Editor' in the 'View' tab. Only thing for you to check is that the name of the table you refer to will be "Table1".
 
Upvote 0
How about
Fluff.xlsm
ABCDEFG
1
2
3
4Swan30Swan
5Goat54Swan
6Duck210Swan
7Dog113 
8Cat315Goat
9 Goat
10 Goat
11Goat
12Goat
13 
14Duck
15Duck
16 
17Dog
18 
19Cat
20 
21 
Summary
Cell Formulas
RangeFormula
E4:E10E4=IF(D4="","",SUM(D$3:D3)+ROWS(E$4:E4)-1)
G4:G21G4=IF(OR(ISNUMBER(MATCH(ROWS(G$4:G4),$E$4:$E$10,0)),ROWS(G$4:G4)>MAX($E$4:$E$10)+1),"",INDEX($C$4:$C$10,MATCH(ROWS(G$4:G4),$E$4:$E$10,1)))
 
Upvote 0
How about
Fluff.xlsm
ABCDEFG
1
2
3
4Swan30Swan
5Goat54Swan
6Duck210Swan
7Dog113 
8Cat315Goat
9 Goat
10 Goat
11Goat
12Goat
13 
14Duck
15Duck
16 
17Dog
18 
19Cat
20 
21 
Summary
Cell Formulas
RangeFormula
E4:E10E4=IF(D4="","",SUM(D$3:D3)+ROWS(E$4:E4)-1)
G4:G21G4=IF(OR(ISNUMBER(MATCH(ROWS(G$4:G4),$E$4:$E$10,0)),ROWS(G$4:G4)>MAX($E$4:$E$10)+1),"",INDEX($C$4:$C$10,MATCH(ROWS(G$4:G4),$E$4:$E$10,1)))
Hi, that works well if I add a 'bogus' extra animal at the end then 'Cat' will populate 3 cells
 
Upvote 0

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

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