• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro to append to cell

Neal Schultz

New Member
Could someone please assist with this via macro?

Column A contains multiple rows of data. The data is the same in consecutive 3 rows:

XYZ45789
XYZ45789
XYZ45789
XYZ45790
XYZ45790
XYZ45790
XYZ45791
XYZ45791
XYZ45791
XYZ45792
XYZ45792
XYZ45792
XYZ45793
XYZ45793
XYZ45793
XYZ45794
XYZ45794
XYZ45794

Macro to loop through the column appending an A to the second instance of same data and a B to the third instance. Continue looping until end of data in column A. To achieve this:

XYZ45789
XYZ45789 A
XYZ45789 B
XYZ45790
XYZ45790 A
XYZ45790 B
XYZ45791
XYZ45791 A
XYZ45791 B
XYZ45792
XYZ45792 A
XYZ45792 B
XYZ45793
XYZ45793 A
XYZ45793 B
XYZ45794
XYZ45794 A
XYZ45794 B
 
Something like this?
Code:
Sub cOccurrence()
Dim cRange As Range
Dim lRow As Integer
Dim vCount As Integer

lRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row

With Sheet1
For i = 1 To lRow
    vCount = Application.WorksheetFunction.CountIf(Range("A1:A" & i), Range("A" & i) & "*")
    If vCount > 1 Then
    Select Case vCount
        Case Is = 1
            Range("A" & i) = Range("A" & i)
        Case Is = 2
            Range("A" & i) = Range("A" & i) & "A"
        Case Is = 3
            Range("A" & i) = Range("A" & i) & "B"
    End Select
    Else
    End If
Next
End With
   

End Sub
 
Absolutely no loop and one liner.
Code:
Sub test()
    [a1:a1000] = [a1:a1000&if(countif(offset(a1:a1000,,,row(1:1000)),a1:a1000)>1,char(63+countif(offset(a1:a1000,,,row(1:1000)),a1:a1000)),"")]
End Sub
 
Back
Top