Need idea(s) on how to minimize code for handling groups of cells

frateg8r

Board Regular
Joined
Mar 2, 2005
Messages
221
Office Version
  1. 365
Platform
  1. Windows
I have a worksheet with unique numbers in each cell in column A; the number of cells with data could be anywhere from 2 to 999. The data would thus look like this:
XXXX0000000
XXXX1111111
XXXX2222222

I will then need to output those values, in groups of not more than 5, to a row elsewhere, such as:
VBA Code:
range("D1").Value = A1 & " " & A2 & " " & A3 & " " & A4 & " " & A5
etc....

Rather than hardcode each possible combination of 5 cells into its own line (as in repeating that line over and over, then manually adjusting the cell addresses for each line...sheesh, that would be tedious), I'm trying to think of how I can accomplish that in the minimal amount of code. I'm sure there is a way to use variables and other if/then or for each/next scenarios to accomplish this, and I will figure it out eventually. But, on the chance that someone might have a ready idea at hand, I would appreciate any insights/direction. I've got a tremendous amount of code I can refer back to, but I just don't recall having done anything quite like this before.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I have a worksheet with unique numbers in each cell in column A; the number of cells with data could be anywhere from 2 to 999. The data would thus look like this:
XXXX0000000
XXXX1111111
XXXX2222222

I will then need to output those values, in groups of not more than 5, to a row elsewhere, such as:
VBA Code:
range("D1").Value = A1 & " " & A2 & " " & A3 & " " & A4 & " " & A5
etc....

Rather than hardcode each possible combination of 5 cells into its own line (as in repeating that line over and over, then manually adjusting the cell addresses for each line...sheesh, that would be tedious), I'm trying to think of how I can accomplish that in the minimal amount of code. I'm sure there is a way to use variables and other if/then or for each/next scenarios to accomplish this, and I will figure it out eventually. But, on the chance that someone might have a ready idea at hand, I would appreciate any insights/direction. I've got a tremendous amount of code I can refer back to, but I just don't recall having done anything quite like this before.
Does this work?

Assumes that the worksheet is named 'Data' but you can change this.

VBA Code:
Public Sub subGroupRows()
Dim Ws As Worksheet
    
    Set Ws = Worksheets("Data")
    
    With Ws.Range("D2:D" & Int(Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row / 5) + 2)
        .Formula2 = "=TEXTJOIN(" & """" & "  " & """" & ",TRUE,OFFSET($A$1,((ROW()-2)*5)+1,0,5,1))"
        .Value = .Value
    End With
            
End Sub
 
Upvote 0
Solution
.Formula2 = "=TEXTJOIN(" & """" & " " & """" & ",TRUE,OFFSET($A$1,((ROW()-2)*5)+1,0,5,1))"
I can get the formula into the target cell, but nothing displays.
  • Can you clarify for me what the (ROW()-2*5) is referring to? I understand the position of the formula is the number of rows, but I'm not clear on if it is referring to the number of rows in the origin data, or the destination range. I also get that ROW() is pulling the row# of the instant cell - yet, I'm not clear on what cell that is intended to be (again, origin or destination). Knowing these things might help me figure this out (this is my first encounter with TEXTJOIN).
 
Upvote 0
I can get the formula into the target cell, but nothing displays.
  • Can you clarify for me what the (ROW()-2*5) is referring to? I understand the position of the formula is the number of rows, but I'm not clear on if it is referring to the number of rows in the origin data, or the destination range. I also get that ROW() is pulling the row# of the instant cell - yet, I'm not clear on what cell that is intended to be (again, origin or destination). Knowing these things might help me figure this out (this is my first encounter with TEXTJOIN).
Never mind! I just needed some time to think of a different approach to figuring this out, and I did. This works perfectly - thank you! I learned something as well, so it's a big win for me.
 
Upvote 0
Perhaps you need vba for your purposes but just in case, this can be done with a single formula in a single cell.
Change the 5 in the WRAPROWS part of the formula for a different number of rows to group.

frateg8r.xlsm
ABCD
1
2X0X0 X1 X2 X3 X4
3X1X5 X6 X7 X8 X9
4X2X10 X11
5X3
6X4
7X5
8X6
9X7
10X8
11X9
12X10
13X11
14
Data
Cell Formulas
RangeFormula
D2:D4D2=BYROW(WRAPROWS(A2:A13,5,""),LAMBDA(rw,TEXTJOIN(" ",,rw)))
Dynamic array formulas.
 
Upvote 1

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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