TylerCross
New Member
- Joined
- Jan 5, 2021
- Messages
- 7
- Office Version
- 2010
- Platform
- Windows
Hello,
I am currently creating some vba code to concatenate rows in a certain column into one. When I run two different data, the first one works but then the second one does not work and I receive a error message. My code is
Sub mergeCategoryValues()
Dim lngRow As Long
With Sheet1
Dim columnToMatch As Integer: columnToMatch = 1
Dim columnToConcatenate As Variant: columnToConcatenate = 22
Dim columnToSum As Integer: columnToSum = 2
lngRow = .Cells(65536, columnToMatch).End(xlUp).Row
.Cells(columnToMatch).CurrentRegion.Sort key1:=.Cells(columnToMatch), Header:=xlYes
Do
If .Cells(lngRow, columnToMatch) = .Cells(lngRow - 1, columnToMatch) Then
.Cells(lngRow - 1, columnToConcatenate) = .Cells(lngRow - 1, columnToConcatenate) & "; " & .Cells(lngRow, columnToConcatenate)
.Cells(lngRow - 1, columnToSum) = .Cells(lngRow - 1, columnToSum) + .Cells(lngRow, columnToSum)
.Rows(lngRow).Delete
End If
lngRow = lngRow - 1
Loop Until lngRow = 1
End With
MsgBox (" Macro Complete! ")
End Sub
The red text is where the error is occurring. I am using a Variant data type in the rows it contains numbers and text.
Any help is greatly appreciated.
I am currently creating some vba code to concatenate rows in a certain column into one. When I run two different data, the first one works but then the second one does not work and I receive a error message. My code is
Sub mergeCategoryValues()
Dim lngRow As Long
With Sheet1
Dim columnToMatch As Integer: columnToMatch = 1
Dim columnToConcatenate As Variant: columnToConcatenate = 22
Dim columnToSum As Integer: columnToSum = 2
lngRow = .Cells(65536, columnToMatch).End(xlUp).Row
.Cells(columnToMatch).CurrentRegion.Sort key1:=.Cells(columnToMatch), Header:=xlYes
Do
If .Cells(lngRow, columnToMatch) = .Cells(lngRow - 1, columnToMatch) Then
.Cells(lngRow - 1, columnToConcatenate) = .Cells(lngRow - 1, columnToConcatenate) & "; " & .Cells(lngRow, columnToConcatenate)
.Cells(lngRow - 1, columnToSum) = .Cells(lngRow - 1, columnToSum) + .Cells(lngRow, columnToSum)
.Rows(lngRow).Delete
End If
lngRow = lngRow - 1
Loop Until lngRow = 1
End With
MsgBox (" Macro Complete! ")
End Sub
The red text is where the error is occurring. I am using a Variant data type in the rows it contains numbers and text.
Any help is greatly appreciated.