Excel VBA 找出選定范圍不重復值和重復值


Sub 找出選定范圍內不重復的值()  
    On Error Resume Next  
  
    Dim d As Object  
    Set d = CreateObject("scripting.dictionary")  
    For c_i = 1 To selection.Columns.Count  
    For Each ce In selection.Columns(c_i).Cells  
        'd.Add ce.Value, 1  
        If ce <> "" Then  
            'D(ce.Value) = ""  
            If d.Exists(ce.Value) Then  
                d(ce.Value) = d(ce.Value) + 1  
            Else  
                d(ce.Value) = 1  
            End If  
        End If  
    Next  
    Next  
    'Debug.Print d.Count  
    If MsgBox("是否在鄰列顯示出現次數?", vbYesNo, "統計次數") = vbYes Then  
        標記 = True  
    End If  
    e = InputBox("希望在那個單元格下生成結果:", "結果輸出", Chr(97 + selection.Columns(1).Cells(1).Column + 2) & selection.Columns(1).Cells(1).row)  
    m = Left(e, 1)  
    jj = Mid(e, 2, 1)  
    Range(m & jj) = "不重復值"  
    If 標記 = True Then  
        Range(Chr((Asc(m) + 1)) & jj) = "頻率" '大於1的值  
    End If  
    For Each Key In d.Keys  
        Range(m & (jj + 1)) = Key  
        If 標記 = True Then  
'            If d(Key) > 1 Then Range(Chr((Asc(m) + 1)) & (jj + 1)) = d(Key)  
    Range(Chr((Asc(m) + 1)) & (jj + 1)) = d(Key)  
        End If  
        jj = jj + 1  
    Next  
    Set d = Nothing  
    '最后進行排序  
'     Range(m & selection.Columns(1).Cells(1).row & ":" & Chr((Asc(m) + 1)) & (jj)).Select  
    Range(e & ":" & Chr((Asc(m) + 1)) & (jj)).Sort Key1:=Range(m & Mid(e, 2, 1) + 1), Order1:=xlAscending, HEADER:=xlYes _  
    , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _  
    SortMethod:=xlPinYin, DataOption1:=xlSortNormal  
End Sub  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM