Hi,
To start off, my question is very similar to the one at this location: http://www.mrexcel.com/forum/excel-questions/553169-concatenate-row-variable-number-columns.html
This is the provided solution that I've been trying to use:
However, the problem is that when I use the solution provided, I receive the error: "Run-time error '13': Type mismatch", and it highlights the line immediately below the "Else" statement. I believe this is because some of the rows in my data do not have data to be concatenated (or, I may be adapting the solution incorrectly by only changing the "B" and "C" values to "E" and "F").
Example:
[TABLE="class: grid, width: 500, align: center"]
<TBODY>[TR]
[TD]Row1[/TD]
[TD][/TD]
[TD]Data1[/TD]
[TD]Data4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Row2[/TD]
[TD][/TD]
[TD]Data1[/TD]
[TD]Data2[/TD]
[TD]Data3[/TD]
[/TR]
[TR]
[TD]Row3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Row4[/TD]
[TD][/TD]
[TD]Data2[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Row5[/TD]
[TD][/TD]
[TD]Data1[/TD]
[TD]Data3[/TD]
[TD][/TD]
[/TR]
</TBODY>[/TABLE]
I would like the second column in the example (which is column E in my spreadsheet) to concatenate all cells that contain data to the right of it. Some of these are as long as 200 cells, but they will always be horizontally adjacent to each other, because I've removed blank cells and shifted the data left.
If it helps, each row will contain a counter to show how many cells are populated in the row, so the macro could reference that if needed.
Thank you in advance!
To start off, my question is very similar to the one at this location: http://www.mrexcel.com/forum/excel-questions/553169-concatenate-row-variable-number-columns.html
This is the provided solution that I've been trying to use:
Code:
Sub ConCatFromColumnC()
Dim X As Long, LastRow As Long, LastCol As Long
Const StartRow As Long = 1
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For X = StartRow To LastRow
LastCol = Cells(X, Columns.Count).End(xlToLeft).Column
If LastCol = 3 Then
Cells(X, "B").Value = Cells(X, "C").Value
Else
Cells(X, "B").Value = Join(Application.Index(Range(Cells(X, "C"), Cells(X, LastCol)).Value, 1, 0), ", ")
End If
Next
End Sub
However, the problem is that when I use the solution provided, I receive the error: "Run-time error '13': Type mismatch", and it highlights the line immediately below the "Else" statement. I believe this is because some of the rows in my data do not have data to be concatenated (or, I may be adapting the solution incorrectly by only changing the "B" and "C" values to "E" and "F").
Example:
[TABLE="class: grid, width: 500, align: center"]
<TBODY>[TR]
[TD]Row1[/TD]
[TD][/TD]
[TD]Data1[/TD]
[TD]Data4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Row2[/TD]
[TD][/TD]
[TD]Data1[/TD]
[TD]Data2[/TD]
[TD]Data3[/TD]
[/TR]
[TR]
[TD]Row3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Row4[/TD]
[TD][/TD]
[TD]Data2[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Row5[/TD]
[TD][/TD]
[TD]Data1[/TD]
[TD]Data3[/TD]
[TD][/TD]
[/TR]
</TBODY>[/TABLE]
I would like the second column in the example (which is column E in my spreadsheet) to concatenate all cells that contain data to the right of it. Some of these are as long as 200 cells, but they will always be horizontally adjacent to each other, because I've removed blank cells and shifted the data left.
If it helps, each row will contain a counter to show how many cells are populated in the row, so the macro could reference that if needed.
Thank you in advance!