'How to Paste string of values in single cell of Excel using VBA

I have 2 sheets named "Target Data" and "Pivot". I have filtered data in "Target Data" and now want to copy only visible cells in single cell of "Pivot" sheet.

I have created temp sheet to paste the visible cells(Filtered). Now want to copy the email from col "F" of temp sheet and paste it in cell"B4" only of "Pivot".

just not able to execute the for loop so that it can hold the all the values in str.

My code:


Sub ccTo()

Dim str As Variant
Dim i As Long
Dim Lrow As Range
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim Temp as Worksheet

Set ws = ThisWorkbook.Worksheets("Target Data")
Set ws1 = ThisWorkbook.Worksheets("Pivot")
ws1.Activate

Range("B5").Select
Selection.Copy

ws.Activate

Range("A1").Select

With Worksheets("Target Data")
        Set Frange = .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column))
        Frange.AutoFilter field:=14, Criteria1:=ws1.Range("b5")
        Range("A1:x1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
End With

Temp.Activate
Range("A1").PasteSpecial xlPasteAll
Range("A1").Select

Lrow = Cells(Rows.Count, 1).End(xlUp).Row

ReDim str(2 To Lrow)

For i = 2 To Lrow

    str(i) = Range("F" & i).Value
    ws1.Activate
    Range("B4").Value = str(i) & ";" + str(i - 1)
    Temp.Activate
Next i

Application.DisplayAlerts = False
Temp.Delete
Application.DisplayAlerts = True

ws1.Activate
Range("B4").Clear
End Sub



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source