Please Help me simplifying this silly formula

jbesclapez

Active Member
Joined
Feb 6, 2010
Messages
275
Hello,

I made a stupid formula. It works but as my data increases it gets impossible. I need a better version of it. No VBA plz.

Code:
=IF(H20=1,I20,IF(H20=2,IF(J20=1,I20&"--"&I21,K19),IF(H20=3,IF(J20=1,I20&"--"&I21&"--"&I22,K19),IF(H20=4,IF(J20=1,I20&"--"&I21&"--"&I22&"--"&I23,K19),IF(H20=5,IF(J20=1,I20&"--"&I21&"--"&I22&"--"&I23&"--"&I24,K19),IF(H20=6,IF(J20=1,I20&"--"&I21&"--"&I22&"--"&I23&"--"&I24&"--"&I25,K19),IF(H20=7,IF(J20=1,I20&"--"&I21&"--"&I22&"--"&I23&"--"&I24&"--"&I25&"--"&I26,K19),"Superieura7")
))))))

Basically the formula concatenates N amount of cells that are below each other.
The N amount to concatenate is on another cell in the same line.
You can have many groups of N.
The X amount helps you differenciate the groups as a new group always starts with 1...to N

In the example below, the tricky one is the group N=5 because there are 2 groups : The first one giving the result KLMNO and the second one PQRST

[TABLE="width: 500"]
<tbody>[TR]
[TD]N[/TD]
[TD]X[/TD]
[TD]data[/TD]
[TD]Expected Result[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1
[/TD]
[TD]A[/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD]B[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD]C[/TD]
[TD]CD[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]2[/TD]
[TD]D[/TD]
[TD]CD[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]1[/TD]
[TD]E[/TD]
[TD]EFH[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]2[/TD]
[TD]F[/TD]
[TD]EFH[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]3[/TD]
[TD]H[/TD]
[TD]EFH[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD]I[/TD]
[TD]I[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD]J[/TD]
[TD]J[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]1[/TD]
[TD]K[/TD]
[TD]KLMNO[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]2[/TD]
[TD]L[/TD]
[TD]KLMNO[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]3[/TD]
[TD]M[/TD]
[TD]KLMNO[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]4[/TD]
[TD]N[/TD]
[TD]KLMNO[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]5[/TD]
[TD]O[/TD]
[TD]KLMNO[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]1[/TD]
[TD]P[/TD]
[TD]PQRST[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]2[/TD]
[TD]Q[/TD]
[TD]PQRST[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]3[/TD]
[TD]R[/TD]
[TD]PQRST[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]4[/TD]
[TD]S[/TD]
[TD]PQRST[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]5[/TD]
[TD]T[/TD]
[TD]PQRST[/TD]
[/TR]
</tbody>[/TABLE]


How can I solve that easily knowing that N can be above 60 :-)

Thanks



[TABLE="width: 500"]
<tbody>[TR]
[TD]PQRST[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
What Excel version are you using?
I'm asking because in Excel 365 the function TEXTJOIN is available and a simple formula can be created. Otherwise, you need VBA.

M.
 
Upvote 0
What Excel version are you using?
I'm asking because in Excel 365 the function TEXTJOIN is available and a simple formula can be created. Otherwise, you need VBA.

M.

Hi Marcello,

I am not using the 365 version.

I kind of guessed your conclusion and prepared that :
Function CONCATENATEMULTIPLE(Ref As Range, Separator As String) As String
Dim Cell As Range
Dim Result As String
For Each Cell In Ref
Result = Result & Cell.Value & Separator
Next Cell
CONCATENATEMULTIPLE = Left(Result, Len(Result) - 2)
End Function

Please note that I am using a double separator (--)

Now working on the magic VBA
 
Upvote 0
Maybe...

Code:
Function Conc(r As Range, sep As String)
    Dim rCell As Range, strAux As String
    
    For Each rCell In r.Offset(, 2).Resize(r.Value)
        strAux = strAux & " " & rCell.Value
    Next rCell
    Conc = Replace(Application.Trim(strAux), " ", sep)
End Function


[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][td="bgcolor: #DCE6F1"]
C
[/td][td="bgcolor: #DCE6F1"]
D
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td]
N​
[/td][td]
X​
[/td][td]
data​
[/td][td]
Expected Result​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
1​
[/td][td]
1​
[/td][td]
A​
[/td][td]
A​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
1​
[/td][td]
1​
[/td][td]
B​
[/td][td]
B​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
2​
[/td][td]
1​
[/td][td]
C​
[/td][td]
C--D​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
5
[/td][td]
2​
[/td][td]
2​
[/td][td]
D​
[/td][td]
C--D​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
6
[/td][td]
3​
[/td][td]
1​
[/td][td]
E​
[/td][td]
E--F--H​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
7
[/td][td]
3​
[/td][td]
2​
[/td][td]
F​
[/td][td]
E--F--H​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
8
[/td][td]
3​
[/td][td]
3​
[/td][td]
H​
[/td][td]
E--F--H​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
9
[/td][td]
1​
[/td][td]
1​
[/td][td]
I​
[/td][td]
I​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
10
[/td][td]
1​
[/td][td]
1​
[/td][td]
J​
[/td][td]
J​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
11
[/td][td]
5​
[/td][td]
1​
[/td][td]
K​
[/td][td]
K--L--M--N--O​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
12
[/td][td]
5​
[/td][td]
2​
[/td][td]
L​
[/td][td]
K--L--M--N--O​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
13
[/td][td]
5​
[/td][td]
3​
[/td][td]
M​
[/td][td]
K--L--M--N--O​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
14
[/td][td]
5​
[/td][td]
4​
[/td][td]
N​
[/td][td]
K--L--M--N--O​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
15
[/td][td]
5​
[/td][td]
5​
[/td][td]
O​
[/td][td]
K--L--M--N--O​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
16
[/td][td]
5​
[/td][td]
1​
[/td][td]
P​
[/td][td]
P--Q--R--S--T​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
17
[/td][td]
5​
[/td][td]
2​
[/td][td]
Q​
[/td][td]
P--Q--R--S--T​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
18
[/td][td]
5​
[/td][td]
3​
[/td][td]
R​
[/td][td]
P--Q--R--S--T​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
19
[/td][td]
5​
[/td][td]
4​
[/td][td]
S​
[/td][td]
P--Q--R--S--T​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
20
[/td][td]
5​
[/td][td]
5​
[/td][td]
T​
[/td][td]
P--Q--R--S--T​
[/td][/tr]
[/table]


Formula in D2 copied down
=IF(B2=1,Conc(A2,"--"),D1)

Hope this helps

M.
 
Upvote 0
Maybe...

Code:
Function Conc(r As Range, sep As String)
    Dim rCell As Range, strAux As String
    
    For Each rCell In r.Offset(, 2).Resize(r.Value)
        strAux = strAux & " " & rCell.Value
    Next rCell
    Conc = Replace(Application.Trim(strAux), " ", sep)
End Function


[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
A
[/TD]
[TD="bgcolor: #DCE6F1"]
B
[/TD]
[TD="bgcolor: #DCE6F1"]
C
[/TD]
[TD="bgcolor: #DCE6F1"]
D
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD]
N​
[/TD]
[TD]
X​
[/TD]
[TD]
data​
[/TD]
[TD]
Expected Result​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD]
1​
[/TD]
[TD]
1​
[/TD]
[TD]
A​
[/TD]
[TD]
A​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD]
1​
[/TD]
[TD]
1​
[/TD]
[TD]
B​
[/TD]
[TD]
B​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD]
2​
[/TD]
[TD]
1​
[/TD]
[TD]
C​
[/TD]
[TD]
C--D​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD]
2​
[/TD]
[TD]
2​
[/TD]
[TD]
D​
[/TD]
[TD]
C--D​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD]
3​
[/TD]
[TD]
1​
[/TD]
[TD]
E​
[/TD]
[TD]
E--F--H​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD]
3​
[/TD]
[TD]
2​
[/TD]
[TD]
F​
[/TD]
[TD]
E--F--H​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
8
[/TD]
[TD]
3​
[/TD]
[TD]
3​
[/TD]
[TD]
H​
[/TD]
[TD]
E--F--H​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
9
[/TD]
[TD]
1​
[/TD]
[TD]
1​
[/TD]
[TD]
I​
[/TD]
[TD]
I​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
10
[/TD]
[TD]
1​
[/TD]
[TD]
1​
[/TD]
[TD]
J​
[/TD]
[TD]
J​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
11
[/TD]
[TD]
5​
[/TD]
[TD]
1​
[/TD]
[TD]
K​
[/TD]
[TD]
K--L--M--N--O​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
12
[/TD]
[TD]
5​
[/TD]
[TD]
2​
[/TD]
[TD]
L​
[/TD]
[TD]
K--L--M--N--O​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
13
[/TD]
[TD]
5​
[/TD]
[TD]
3​
[/TD]
[TD]
M​
[/TD]
[TD]
K--L--M--N--O​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
14
[/TD]
[TD]
5​
[/TD]
[TD]
4​
[/TD]
[TD]
N​
[/TD]
[TD]
K--L--M--N--O​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
15
[/TD]
[TD]
5​
[/TD]
[TD]
5​
[/TD]
[TD]
O​
[/TD]
[TD]
K--L--M--N--O​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
16
[/TD]
[TD]
5​
[/TD]
[TD]
1​
[/TD]
[TD]
P​
[/TD]
[TD]
P--Q--R--S--T​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
17
[/TD]
[TD]
5​
[/TD]
[TD]
2​
[/TD]
[TD]
Q​
[/TD]
[TD]
P--Q--R--S--T​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
18
[/TD]
[TD]
5​
[/TD]
[TD]
3​
[/TD]
[TD]
R​
[/TD]
[TD]
P--Q--R--S--T​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
19
[/TD]
[TD]
5​
[/TD]
[TD]
4​
[/TD]
[TD]
S​
[/TD]
[TD]
P--Q--R--S--T​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
20
[/TD]
[TD]
5​
[/TD]
[TD]
5​
[/TD]
[TD]
T​
[/TD]
[TD]
P--Q--R--S--T​
[/TD]
[/TR]
</tbody>[/TABLE]


Formula in D2 copied down
=IF(B2=1,Conc(A2,"--"),D1)

Hope this helps

M.

Thanks, yuor solution works. THumbs up.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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