2023年7月12日 星期三

VBA_列出mail地址

 A工作表的B欄是收件人的mail位址,不同收件人以,分隔,C欄顯示mail位置,D欄顯示mail出現次數,E欄顯示抓取@之後的內容。


VBA代碼如下:



Sub CountRecipients()

    Dim ws As Worksheet

    Dim lastRow As Long

    Dim recipients As String

    Dim recipientList() As String

    Dim recipient As Variant

    Dim dict As Object

    

    Set ws = ThisWorkbook.Sheets("A") ' 將 "A" 替換為你的工作表名稱

    

    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

    

    Set dict = CreateObject("Scripting.Dictionary")

    

    ' 遍歷B欄中的每個儲存格,將收件人位址以逗號分隔的方式拆分並統計數量

    For i = 1 To lastRow

        recipients = ws.Cells(i, "B").Value

        recipientList = Split(recipients, ",")

        For Each recipient In recipientList

            recipient = Trim(recipient)

            If recipient <> "" Then

                If Not dict.Exists(recipient) Then

                    dict(recipient) = 1

                Else

                    dict(recipient) = dict(recipient) + 1

                End If

            End If

        Next recipient

    Next i

    

    ' 將統計結果輸出到C、D和E欄

    ws.Range("C1").Value = "Mail Address"

    ws.Range("D1").Value = "Count"

    ws.Range("E1").Value = "Domain"

    i = 2

    For Each recipient In dict.Keys

        ws.Cells(i, "C").Value = recipient

        ws.Cells(i, "D").Value = dict(recipient)

        ws.Cells(i, "E").Value = Mid(recipient, InStr(recipient, "@") + 1)

        i = i + 1

    Next recipient

    

    Set dict = Nothing

End Sub


沒有留言:

張貼留言

用GPT修改M語法_以日期列處理

在2023年GPT開始使用時我就詢問過GPT,可能當時的GPT還不夠聰明,給予的答案無法運行,我認為原始的語法應該可以更聰明點,詢問過Power BI社群的人,但沒有人提供答案,終於在現在再度詢問GPT給予的答案與我思考的方向依樣,只是我懂得語法不多,透過GPT幫忙解答,我也學到...