投資觀察清單中有時候會不小心多列到了重複的項目
需要一點幫助,提醒自己有填到重複
放上練習檔,給需要的網友參考
設計時主要是會將股號放在 column A
找出column A 中重複的項目
並且在 column C顯示提醒字串
基本上主要功能就是有找出重複項
若有三個項目或以上重複,也沒關係,有出現提醒即可
設計想法,舉例五個clells排一列來看
'cells(1,1)跟其Cells(2, 1)~Cells(5, 1)項目比對
'Cells(1, 1) = Cells(1, 1)'要排除
' Cells(1, 1) = Cells(2, 1)
' Cells(1, 1) = Cells(3, 1)
' Cells(1, 1) = Cells(4, 1)
' Cells(1, 1) = Cells(5, 1)
'cells(2,1)跟其Cells(3, 1)~Cells(5, 1),還有cells(1,1)項目比對
' Cells(2, 1) = Cells(1, 1)
' Cells(2, 1) = Cells(2, 1)'要排除
' Cells(2, 1) = Cells(3, 1)
' Cells(2, 1) = Cells(4, 1)
' Cells(2, 1) = Cells(5, 1)
Sub findrepeat()
'比較前要先把C欄位清空,這樣後面才好放值
'clear column C
Range(Cells(1, 3), Cells(30, 3)).ClearContents
'j is row nuumber,k is other row number for compared
'j是比別人的row,k是被比的row
'總共30個項目要比較
For j = 1 To 30
For k = 1 To 30
'*條件說明*'
'j <> k 就是排除項,自己不要比到自己
'Cells(j, 1) = Cells(k, 1) 就是比較相同
'(Cells(j, 1) <> "") And (Cells(k, 1) <> "") 就是不要比到空格
'*條件說明*'
If (j <> k) And (Cells(j, 1) = Cells(k, 1)) And (Cells(j, 1) <> "") And (Cells(k, 1) <> "") Then
For temp = 1 To 30
Dim myarray(30) As String '設定矩陣長度30格
myarray(temp) = "找到重複項,ROW" & j & "跟ROW" & k & "一樣喔"
Cells(j, 3) = myarray(temp) '把提示顯示在重複的ROW旁
Next
End If
Next
Next
Cells(1, 1).Select '沒特別用途,只是把格子回到A1
End Sub
DEMO VBA 下載連結
0 留言