Excel 2013 VBA loop: How to add to text in cell

jedwardo

Board Regular
Joined
Aug 21, 2012
Messages
122
Hi Everyone,

I have this loop I'm building and there's one part (highlighted blue) where it takes all cells with the color index of 46 and places their values in this cell Sheets(1).Cells(2, 81).Value. At the moment each one overwrites the previous. What I'm looking for is for the text to accumulate in that cell separated by a comma for each new addition

Code:
Sub TestCode()
Dim iCell As Range, ws As Worksheet
Set ws = Sheet6
Lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    For Each iCell In Range("A2:CA30")
        With iCell
        If iCell.Interior.ColorIndex = 35 Then
            ws.Cells(Lastrow + 1, 1).Value = iCell.Value
            ws.Cells(Lastrow + 1, 1).Offset(0, 1).Value = iCell.Offset(0, 1).Value
            Lastrow = Lastrow + 1
        ElseIf iCell.Value <> "" And iCell.Interior.ColorIndex = 3 Then
            ws.Cells(Lastrow + 1, 3).Value = Sheets(1).Cells(1, iCell.Column).Value
            ws.Cells(Lastrow + 1, 10).Value = iCell.Value
            Lastrow = Lastrow + 1
        ElseIf iCell.Value <> "" And iCell.Interior.ColorIndex = 24 Then
            ws.Cells(Lastrow + 1, 3).Value = Sheets(1).Cells(1, iCell.Column).Value
            ws.Cells(Lastrow + 1, 6).Value = iCell.Value
            ws.Cells(Lastrow + 1, 8).Value = iCell.Offset(0, 1).Value
            Lastrow = Lastrow + 1
        ElseIf iCell.Value > "0" And iCell.Interior.ColorIndex = 40 Then
            ws.Cells(Lastrow + 1, 8).Value = Sheets(1).Cells(1, iCell.Column).Value
            ws.Cells(Lastrow + 1, 10).Value = iCell.Value
            Lastrow = Lastrow + 1
[COLOR=#0000ff]        ElseIf iCell.Value > "0" And iCell.Interior.ColorIndex = 46 Then[/COLOR]
[COLOR=#0000ff]            Sheets(1).Cells(2, 81).Value = Sheets(1).Cells(1, iCell.Column).Value[/COLOR]
        ElseIf iCell.Value > "0" And iCell.Interior.ColorIndex = 48 Then
            ws.Cells(Lastrow + 1, 8).Value = Sheets(1).Cells(1, iCell.Column).Value
            ws.Cells(Lastrow + 1, 10).Value = iCell.Offset(0, 1).Value
            Lastrow = Lastrow + 1
        End If[COLOR=#00ffff][/COLOR][COLOR=#40e0d0][/COLOR]
        End With
    Next iCell
End Sub

Thanks,
Jordan
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You can concatenate text in vb using the & operator.

To add in commas, do something like this:

Sheets(1).Cells(2,81).Value = Sheets(1).Cells(2,81).Value & ", " & string2

etc.
 
Upvote 0
Maybe

Code:
Sheets(1).Cells(2, 81).Value = Sheets(1).Cells(2, 81).Value & "," & Sheets(1).Cells(1, iCell.Column).Value
 
Upvote 0
Jordan,

Try replacing with....

Rich (BB code):
ElseIf iCell.Value > "0" And iCell.Interior.ColorIndex = 46 Then
If Sheets(1).Cells(2, 81).Value = "" Then
    Sheets(1).Cells(2, 81).Value = Sheets(1).Cells(1, iCell.Column).Value
    Else
    Sheets(1).Cells(2, 81).Value = Sheets(1).Cells(2, 81).Value & "," & Sheets(1).Cells(1, iCell.Column).Value
End If



Hope that helps.
 
Upvote 0
Wow, you guys are great thanks. I feel stupid though I didn't foresee something obvious. Using the last code from snakehips the appropriate cells ended up in the designated area but then all other values from the other rows were appended as well. Is it possible to make the target destination go down the column one row as the loop goes through the rows?


28bb535.png
[/IMG]

So in this example BE2 and BF2's header values would get transfered to CC2 like the code told them to but then in the next row BE3 would go to CC3, BE4 to CC4 and so on.
 
Upvote 0
Nevermind I figured that part out. Changed the cell 2 in the code to iCell.row. Thanks again everyone

Code:
If Sheets(1).Cells([COLOR=#0000ff]iCell.Row[/COLOR], 81).Value = "" Then                
                Sheets(1).Cells([COLOR=#0000ff]iCell.Row[/COLOR], 81).Value = Sheets(1).Cells(1, iCell.Column).Value
                Else
                Sheets(1).Cells([COLOR=#0000ff]iCell.Row[/COLOR], 81).Value = Sheets(1).Cells([COLOR=#0000ff]iCell.Row[/COLOR], 81).Value & "," & Sheets(1).Cells(1, iCell.Column).Value
                Lastrow = Lastrow + 1
 
Upvote 0
Try referencing the row like this:

ElseIf iCell.Value > "0" And iCell.Interior.ColorIndex = 46 Then
If Sheets(1).Cells(iCell.Row, 81).Value = "" Then
Sheets(1).Cells(iCell.Row, 81).Value = Sheets(1).Cells(1, iCell.Column).Value
Else
Sheets(1).Cells(iCell.Row, 81).Value = Sheets(1).Cells(iCell.Row, 81).Value & "," & Sheets(1).Cells(1, iCell.Column).Value
End If


THIS CODE HAS NOT BEEN TESTED, BACKUP YOUR DATA
 
Upvote 0
You beat me too it, nice work on figuring it out on your own. That's the feeling that makes it all worth it :)
 
Upvote 0

Forum statistics

Threads
1,217,346
Messages
6,136,030
Members
449,979
Latest member
trinitybg10

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