Home » excel » vba – Adding multiple values in one cell

vba – Adding multiple values in one cell

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have this piece of code

Sub neviem()

Dim ws As Worksheet
Dim i As Range
Dim j As Long

Set i = Range("GKC")

For j = i.Rows.Count To 1 Step -1

    If IsEmpty(Range("E3").Value) Then
        If i(j, 1) Like Range("E2") Then
           i(j, 1).Offset(0, 1).Copy Range("E2").Offset(1, 0)
        End If

    ElseIf i(j, 1) Like Range("E2") Then
        i(j, 1).Offset(0, 1).Copy Range("E2").Offset(1, 0) & "," & Range("E2").Value
    End If

Next

End Sub

With this code I’m trying to add multiple text values in the same cell. The first part is ok when I run it, it will add a text value. The problem is when I run it for a second time it gives me an error

runtime err 1004 copy method class failed

so I’m not able to put more text values next to the one I already have.

Is this possible in VBA?

How to&Answers:

Instead this i(j, 1).Offset(0, 1).Copy Range("E2").Offset(1, 0) & "," & Range("E2").Value

Try this i(j, 1).Offset(0, 1) = Range("E3") & "," & Range("E2")