Split cell contents in different sets

Pete2020

Board Regular
Joined
Apr 25, 2020
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
I am looking for a Excel Formula or function or VBA which is simple to understand. The actual data is Text and not a Numeric values

The cell contents is to be divided into different sets with flower brackets and a comma separator.
It should separate the data equally based on values in available based on "|" delimiter

Ex: Column A Row 1 ( Full range has around 10k Rows)
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|

After Applying formula or VBA code - The values shoule be equally divided and if not divisible it should fall under another curly bracket

In Column B Row1 the data should present with curly Brackets based on groups i mentioned in VBA or formula
{1|2|3|4|5},{6|7|8|9|10},{11|12|13|14|15},{16|17|18|19|20}
 
Here the shortest macro:

In this line you can set the number of elements for each group
If j Mod 5 = 0 Then

VBA Code:
Sub splitCell()
  Dim a As Variant, b As Variant, c As String
  Dim i As Long, j As Long, sItems As Variant
  a = Range("A1", Range("A" & Rows.Count).End(3)).Value2
  ReDim b(1 To UBound(a), 1 To 1)
  '
  For i = 1 To UBound(a)
    sItems = Split(a(i, 1), "|")
    c = "{"
    For j = 0 To UBound(sItems) - 1
      If j Mod 5 = 0 Then c = Left(c, Len(c) - 1) & "},{"
      c = c & sItems(j) & "|"
    Next
    b(i, 1) = Mid(c, 3, Len(c) - 3) & "}"
  Next
  '
  Range("B1").Resize(UBound(a)).Value = b
End Sub
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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