repeat/concat data in a cell based on values in other cells

Troyrob

New Member
Joined
Mar 30, 2018
Messages
4
I am trying to fill in/update cells based on the contents of a data cell and repeat that pattern for N times where N is the value of another cell. The data cell would contain a string and the match condition would be a the contents of another cell. For example:

OutputAAA
DataABCDEFQSXVVV
Repeat3
Match stringA

<tbody>
</tbody>

OutputAAAAAA
DataABCARESY1WE
Repeat3
Match StringA

<tbody>
</tbody>

OutputAAAAAAAAA
DataAA1ABCQweKK
Repeat3
Match StringA

<tbody>
</tbody>

OutputBBBBBBBBB
DataBOOABOBNBV456
Itterations3
Match StringB

<tbody>
</tbody>

I don't know if this is possible to do with just formulas, or if it requires VB, or maybe not even possible in general. Hopefully I explained it well.

Thanks in advance for your input. <style><!--table {mso-displayed-decimal-separator:"\."; mso-displayed-thousand-separator:"\,";}@page {margin:.75in .7in .75in .7in; mso-header-margin:.3in; mso-footer-margin:.3in;}tr {mso-height-source:auto;}col {mso-width-source:auto;}br {mso-data-placement:same-cell;}td {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:black; font-size:12.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:Calibri, sans-serif; mso-font-charset:0; mso-number-format:General; text-align:general; vertical-align:bottom; border:none; mso-background-source:auto; mso-pattern:auto; mso-protection:locked visible; white-space:nowrap; mso-rotate:0;}--></style>
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Welcome to the board.

It seems possible, but I'm not entirely sure of your requirements. Consider this:

ABCDEFGHI
1OutputAAA
2DataABCDEFQSXVVV
3Repeat3
4Match stringA
5
6OutputAAAAAA
7DataABCARESY1WE
8Repeat3
9Match StringA
10
11OutputAAAAAAAAA
12DataAA1ABCQweKK
13Repeat3
14Match StringA
15
16OutputBBBBBBBBBBBB
17DataBOOABOBNBV456
18Itterations3
19Match StringB

<tbody>
</tbody>
Sheet4

Worksheet Formulas
CellFormula
D1=REPT($D4,LEN(B2)-LEN(SUBSTITUTE(B2,$D4,"")))&REPT($D4,LEN(C2)-LEN(SUBSTITUTE(C2,$D4,"")))&REPT($D4,LEN(D2)-LEN(SUBSTITUTE(D2,$D4,"")))

<tbody>
</tbody>

<tbody>
</tbody>



I added 2 empty columns B:C to make this work. You can hide them if you want. Then put in the D1 formula, and drag to the right as far as needed. Then copy to D6:I6, D11:I11, and D16:I11. Notice that my results in G16:I16 don't match yours. So either you have a few typos, or I don't understand your question.

Let me know.


Edit: Shoot, I just realized that you said N times, and this formula is hardcoded for N=3. To do that, you'd need either the new CONCAT function (in some versions of Excel 2016 or Live), or a VBA function. I'll see if I can get a VBA function for you.
 
Last edited:
Upvote 0
The 4th example is a bit of a twist - the match string occurs multiple times in the data, so ideally it would trigger the repeating output once for each match. So if you were to 'unroll' the output it would look like this:

B B B
(no matches)
BB BB BB
B B B
(no matches)
(no matches)

and when you roll them back up the output is:

B B BBB BBB BBB B

My apologies if I am not describing that well. Your reply is very helpful, it is way beyond my current skill level ;)

Thank you
Troy
 
Upvote 0
So the results for your 4th example are what I had in post # 2. So I guess I did see what you wanted. Let's try a UDF. Open a copy of your workbook. Press Alt-F11 to open the VBA editor. From the menu, click Insert > Module. In the sheet that opens, paste this code:

Code:
Public Function MyConcat(MyStr As String, MyCnt As Long, MyRng As Range) As String
Dim i As Long, MyTot As Long, c As Range

    If MyRng.Rows.Count > 1 And MyRng.Columns.Count > 1 Then
        MyConcat = "The range must be 1-D"
        Exit Function
    End If
    
    MyTot = 0
    i = 1
    For Each c In MyRng
        If i >= MyRng.Cells.Count - MyCnt + 1 Then MyTot = MyTot + Len(c.Value) - Len(Replace(c.Value, MyStr, ""))
        i = i + 1
    Next c
    
    MyConcat = WorksheetFunction.Rept(MyStr, MyTot)

End Function
Close the VBA editor (Alt-Q or the red X). Now go to your sheet. Assuming it looks like this:

ABCDEFG
1OutputAAA
2DataABCDEFQSXVVV
3Repeat3
4Match stringA
5
6OutputAAAAAA
7DataABCARESY1WE
8Repeat3
9Match StringA
10
11OutputAAAAAAAAAAAA
12DataAA1ABCQweKK
13Repeat4
14Match StringA
15
16OutputBBBBBBBBBBBB
17DataBOOABOBNBV456
18Itterations3
19Match StringB

<tbody>
</tbody>
Sheet5

Worksheet Formulas
CellFormula
B1=MyConcat($B4,$B3,$B2:B2)

<tbody>
</tbody>

<tbody>
</tbody>



You can use the formula in B1. As you drag it to the right, you'll get the results you want. Copy B1 to the other cells as needed. Let me know how it works.
 
Upvote 0
I am using the latest version of Excel if that helps...
As Eric mentioned, this means you could use a worksheet function using CONCAT if you want.

IF the 'Match string' is always only a single character, try this copied across.

Excel Workbook
ABCDEF
1OutputAAA
2DataABCDEFQSXVVV
3Repeat3
4Match stringA
5
6OutputAAAAAA
7DataABCARESY1WE
8Repeat3
9Match StringA
10
11OutputAAAAAAAAA
12DataAA1ABCQweKK
13Repeat3
14Match StringA
15
16OutputBBBBBBBBBBB
17DataBOOABOBNBV456
18Itterations3
19Match StringB
20
21OutputBBBBBBBBBBBB
22DataBOOABOBNBV456
23Itterations4
24Match StringB
Repeat



IF the 'Match string' can be multiple characters, then change to ..

Excel Workbook
ABCDEF
1OutputABABABABABAB
2DataABCDEFQSXABVVV
3Repeat4
4Match stringAB
Repeat (2)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,225
Members
448,951
Latest member
jennlynn

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