collection values to a single cell

skacutter

New Member
Joined
Dec 23, 2016
Messages
18
I have a collection in VBA of file paths. I need to record each value in the collection to a single cell with each value on a new line in the cell. Possible????

existing code

Code:
Dim colFiles As New Collection

If ListBox3.ListCount > -1 Then


For i = 0 To ListBox3.ListCount - 1
UserChanges = UserChanges & vbCrLf & ListBox3.List(i) & "      " & ListBox3.List(i, 1) & "     " & ListBox3.List(i, 2) & "     " & ListBox3.List(i, 3)
Attachments1 = ListBox3.List(i, 3)
colFiles.Add Attachments1


Next i


End If

Followed by

Code:
If colFiles.Count > 0 Then            For i = 1 To colFiles.Count
                Cells(Rev_Row, "S").Value.???Add???? colFiles(i)
            Next i
        End If

Thank You
Chris
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I m not fully understanding what you are doing with the code you provided but I think this might help you.
When I tried to copy and run this I got n error it did not want to process this line but I don't see where UserChanges is called out

Code:
UserChanges = UserChanges & vbCrLf & ListBox3.List(i) & "      " & ListBox3.List(i, 1) & "     " & ListBox3.List(i, 2) & "     " & ListBox3.List(i, 3)
Attachments1 = ListBox3.List(i, 3)
colFiles.Add Attachments1


This will take all the values in your listbox and display them in a single cell, each item in your listbox will be displayed on a new line. From your code it looks like you have a listbox with 4 columns.

Might be enough to get you in the right direction.

Code:
Sub Tester2()
    OnRow = 1
    'If colFiles.Count > 0 Then
    For e = 0 To ListBox3.ListCount - 1
                
                Dim Var1 As String
                Var1 = ListBox3.List(e, 0) & ListBox3.List(e, 1) & ListBox3.List(e, 2) & ListBox3.List(e, 3)
                Var2 = Range("A1").Value
                Range("A1").Value = Var1
                Range("A2").Value = Var1 & vbNewLine & Var2
                OnRow = OnRow + 1
                Next e
      
End Sub
 
Upvote 0
I m not fully understanding what you are doing with the code you provided but I think this might help you.
When I tried to copy and run this I got n error it did not want to process this line but I don't see where UserChanges is called out

Code:
UserChanges = UserChanges & vbCrLf & ListBox3.List(i) & "      " & ListBox3.List(i, 1) & "     " & ListBox3.List(i, 2) & "     " & ListBox3.List(i, 3)
Attachments1 = ListBox3.List(i, 3)
colFiles.Add Attachments1


This will take all the values in your listbox and display them in a single cell, each item in your listbox will be displayed on a new line. From your code it looks like you have a listbox with 4 columns.

Might be enough to get you in the right direction.

Code:
Sub Tester2()
    OnRow = 1
    'If colFiles.Count > 0 Then
    For e = 0 To ListBox3.ListCount - 1
                
                Dim Var1 As String
                Var1 = ListBox3.List(e, 0) & ListBox3.List(e, 1) & ListBox3.List(e, 2) & ListBox3.List(e, 3)
                Var2 = Range("A1").Value
                Range("A1").Value = Var1
                Range("A2").Value = Var1 & vbNewLine & Var2
                OnRow = OnRow + 1
                Next e
      
End Sub

This worked very well. You hard coded the target cells and all I changed was to make the row dynamic and reuse the code for each column in the listbox-3. Now for each row in the listbox I can record multiple row values to a single row in excel separated by columns

Thank You.

Chris
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top