- Avoid the use of
xlDown
. You may want to see Error in finding last used cell in Excel with VBA
- Avoid the use of
.Text
. You may want to see What is the difference between .text, .value, and .value2?
- What if last row is 1? Handle that as well.
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long, i As Long
'~~> Change this to the relevant worksheet
Set ws = Sheet1
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Check if last row is 1
If lRow = 1 Then
.Range("B1").Value = .Range("A1").Value
Exit Sub
End If
For i = 1 To (lRow - 1)
.Range("B" & i).Value = .Range("A" & i).Value & ","
Next i
End With
End Sub
Here is another way which doesn't use a loop
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Dim sAddr As String
'~~> Change this to the relevant worksheet
Set ws = Sheet1
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Check if last row is 1
If lRow = 1 Then
Range("B1").Value = .Range("A1").Value
Exit Sub
End If
sAddr = "A1:A" & (lRow - 1)
.Range("B1:B" & (lRow - 1)).Value = _
Evaluate("index(concatenate(" & sAddr & "," & """,""" & "),)")
.Range("B" & lRow).Value = .Range("A" & lRow).Value
End With
End Sub
Explanation about index(concatenate())