Condense multiples into first occurrence only

nguerra

New Member
Joined
Oct 1, 2013
Messages
46
Is there a way to take the first occurrence in every value change in a column and re-write it with another value in the same row? For example:

Column 1Column 2Column 6Column 7
appleaapplea
appleapeara
appleaorangeb
pearagrapea
pearabananab
orangebappleb
grapeaorangea
grapea
bananab
appleb
appleb
orangea

<colgroup><col style="width:54pt" span="6" width="72"> </colgroup><tbody>
</tbody>
 
See if this does what you need

Code:
Sub FirstOnlyv2()
    Dim lastRow As Long, i As Long, v As Variant
    
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    With CreateObject("Scripting.Dictionary")
        .comparemode = vbTextCompare
        
        For i = 1 To lastRow
            If Not .exists(Cells(i, 1).Value) Then .Add Cells(i, 1).Value, Range(Cells(i, 2), Cells(i, 3)).Value
        Next i
        
        Range("F1").Resize(.Count).Value = Application.Transpose(.keys)
        
        i = 1
        For Each v In .keys
            Range("G" & i).Resize(, 2).Value = .Item(v)
            i = i + 1
        Next v
    End With
End Sub

M.
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
That's it, thank you. I was trying to modify your original code and I had the Cells(i, 3) right but I didn't know about using the range. I greatly appreciate your help with this
 
Upvote 0
That's it, thank you. I was trying to modify your original code and I had the Cells(i, 3) right but I didn't know about using the range. I greatly appreciate your help with this

You are welcome. Glad for helping.

M.
 
Upvote 0

Forum statistics

Threads
1,215,085
Messages
6,123,030
Members
449,092
Latest member
ikke

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