2016-02-13 46 views
0

我不知道如何創建宏來查找字符的重複項。查找字符的重複項

此圖顯示預期結果。

enter image description here

這張照片是我的實際結果。

enter image description here

這是我嘗試代碼:

Sub atrod_dublikatus() 
' atrod_dublikatus Macro 

Dim a, b, skaits As Integer 
skaits = 0 

For a = 1 To 11 
    For b = a + 1 To 11 

     If Cells(a, 1).Value = Cells(b, 1) Then 
      skait = skait + 1 
      Cells(a, 2).Value = skait 
      Cells(b, 2).Value = skait 

     End If 
    Next b 
Next a 
End Sub 

回答

0

你不需要VBA對於這一點,你可以用一個公式,並不斷擴大範圍做(注意$符號的位置僅在一個系列的一部分)

=IF(COUNTIF(A$1:A1,A2)>0,VLOOKUP(A2,A:B,2,0),IF(COUNTIF(A:A,A2)>1,MAX(B$1:B1)+1,"")) 

放入A1標題(需要使用此公式工作)

把公式放入B2中,然後向下拖動,見下面我的測試數據

Heading 
Hello 1 
World 2 
Blah 3 
Off 
On 
Blah 3 
Blah 3 
World 2 
Blah 3 
Hello 1