I'm trying to write a code that would help me automate the process of combining multiple rows into a single row through the use of VBA. I have several hundreds of grouped data that needs to be put in their own individual rows. The code I currently have with the help of online search allows me to select the range of data that I want and select the location that I want to put it in. This combines all the selected range of data and put all of them in order into a single line.
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">
</code>I don't know how to write it so that it would automatically select the data range that I need and place it where I want it to be repeatedly for a large amount of data. Another problem I'm encountering that I don't know how to write is to make it detect empty rows so that it would skip to the next data cluster.![The photo shows the data clusters that I want to put in one row for each grouping][1] based on the image I would like to put rows 1-4 together into one line as a group.
imgur: the simple image sharer
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">
Code:
[COLOR=#00008B]Sub[/COLOR] TransposeSpecial() [COLOR=#00008B]Dim[/COLOR] lMaxRows [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Long[/COLOR] [COLOR=#808080]'max rows in the sheet[/COLOR]
[COLOR=#00008B]Dim[/COLOR] lThisRow [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Long[/COLOR] [COLOR=#808080]'row being processed[/COLOR]
[COLOR=#00008B]Dim[/COLOR] iMaxCol [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Integer[/COLOR] [COLOR=#808080]'max used column in the row being processed[/COLOR]
[COLOR=#00008B]Dim[/COLOR] Counter [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Integer[/COLOR] [COLOR=#808080]' counter for seperate If loop[/COLOR]
lMaxRows = Cells(Rows.Count, [COLOR=#800000]"A"[/COLOR]).[COLOR=#00008B]End[/COLOR](xlUp).Row
lThisRow = [COLOR=#800000]1[/COLOR] [COLOR=#808080]'start from row 1[/COLOR]
Counter = [COLOR=#800000]1[/COLOR]
[COLOR=#00008B]Do[/COLOR] [COLOR=#00008B]While[/COLOR] lThisRow < lMaxRows
iMaxCol = Cells(lThisRow, Columns.Count).[COLOR=#00008B]End[/COLOR](xlToLeft).Column
[COLOR=#00008B]If[/COLOR] (iMaxCol > [COLOR=#800000]1[/COLOR])
[COLOR=#00008B]Then[/COLOR] [COLOR=#00008B]Call[/COLOR] TransformOneRow
[COLOR=#00008B]End[/COLOR] [COLOR=#00008B]If
[/COLOR]lThisRow = lThisRow + [COLOR=#800000]1
[/COLOR][COLOR=#00008B]Loop
[/COLOR] [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Sub[/COLOR]
[COLOR=#00008B]Sub[/COLOR] TransformOneRow()
[COLOR=#00008B]Dim[/COLOR] InputRng [COLOR=#00008B]As[/COLOR] Range, OutRng [COLOR=#00008B]As[/COLOR] Range
xTitleId = [COLOR=#800000]"Transform"[/COLOR]
[COLOR=#00008B]Set[/COLOR] InputRng = Application.Selection
[COLOR=#00008B]Set[/COLOR] InputRng = Application.InputBox([COLOR=#800000]"Ranges to be transform :"[/COLOR], xTitleId, InputRng.Address, Type:=[COLOR=#800000]8[/COLOR])
[COLOR=#00008B]Set[/COLOR] OutRng = Application.InputBox([COLOR=#800000]"Paste to (single cell):"[/COLOR], xTitleId, Type:=[COLOR=#800000]8[/COLOR])
Application.ScreenUpdating = [COLOR=#800000]False[/COLOR]
xRows = InputRng.Rows.Count
xCols = InputRng.Columns.Coun
t [COLOR=#00008B]For[/COLOR] i = [COLOR=#800000]1[/COLOR] [COLOR=#00008B]To[/COLOR] xRows
InputRng.Rows(i).Copy OutRng
[COLOR=#00008B]Set[/COLOR] OutRng = OutRng.Offset([COLOR=#800000]0[/COLOR], xCols + [COLOR=#800000]0[/COLOR])
[COLOR=#00008B]Next[/COLOR]
Application.ScreenUpdating = [COLOR=#800000]True
[/COLOR][COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Sub[/COLOR]
</code>I don't know how to write it so that it would automatically select the data range that I need and place it where I want it to be repeatedly for a large amount of data. Another problem I'm encountering that I don't know how to write is to make it detect empty rows so that it would skip to the next data cluster.![The photo shows the data clusters that I want to put in one row for each grouping][1] based on the image I would like to put rows 1-4 together into one line as a group.
imgur: the simple image sharer
Last edited: