There is a data set consisting of subsets and contained vertically in excel sheet. Each subset has different row length but the same column length. Each subset has an indicator keyword "group1" in Column"A".
I want each data set to be horizontally aligned next to each other.
For example, say the whole data in a range (A1:M3084) consist of various blocks of x rows and 13 columns. The first block is in (A1:M124), the second data block is in (A125:M250), and so on. I want to cut the second block and paste it next to the first block, leaving a column space (Column"N") in between, to (O1:AA248). Then, repeat the process until the end of the row, keep pasting the next block (A241: M372) next to the second block, to (AC1:AO372), and so forth...
For this, I need to find the row number that contains "group1", which is at the start of subset (top-left cell), and use the (row_number -1) as a blocksize, and repeat the process.
I need to loop through finding each blocksize, and then copy-paste the block next to each other (horizontally).
I have been working on the code but without success (and not sure how to loop these 2 requirements).
Thank you.
Dim rowCount As Integer, colCount As Integer
Dim blockSize As Variant
Dim colOffset As Variant
Dim wordCount As Integer
'Find how many occurance of "group1"
wordCount = Application.WorksheetFunction.CountIf(ActiveSheet.Cells, "group1")
'Find each block size by looping
Dim FindRow As Variant
Do Until FindRow Is Nothing
FindRow = Columns("A").Find(What:="group1").Row ' If text is "group1".
If FindRow > 1 Then 'ignore the 1st block ("group1" inrow(1))
blockSize = FindRow - 1 'row_number-1 = blocksize
'count rows and columns with data
rowCount = Range("A1").CurrentRegion.Rows.Count
colCount = Range("A1").CurrentRegion.Columns.Count
'move the 2nd block and paste it next to the 1st block, delete the block in origin, and continue until the last block
For i = 1 To wordCount
'column number to move
colOffset = i * (colCount + 1)
'move to next column block (@row1) = data in the block @ orign
Range(Cells(1, 1), Cells(blockSize, colCount)).Offset(0, colOffset).Value2 = Range(Cells(1, 1), Cells(blockSize, colCount)).Offset(blockSize * i, 0).Value2 'Value2 returns cerial number
'clear the block in origin
Range(Cells(1, 1), Cells(blockSize, colCount)).Offset(blockSize * i, 0).ClearContents
Loop
Next i
End If
question from:
https://stackoverflow.com/questions/65939096/cut-and-paste-range-repeatedly-with-varying-rows-determined-by-a-specific-strin 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…