首页 > 解决方案 > 将用户名(数组)与电子邮件(数组)匹配

问题描述

使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。

我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。

使用两个字符串:1 个用于用户(1 到 3),另一个用于 eaddress(1 到 3)。我想获取当前用户的电子邮件地址,以便在单独的 Sub 中使用,以便在电子邮件中抄送当前用户。

我尝试了一个For Each i In user,并将 eName 设置为eaddress(i). 这仅返回最后列出的用户/电子邮件。

Private Sub (useremail)
Dim user (1 To 3), eaddress (1 To 3), fullName, eName As String

fullName = Application.UserName
user(1) = "John Smith"
user(2) = "Debbie Adams"
user(3) = "Karen Jones"

eaddress(1) = "jsmith@work.com"
eaddress(2) = "dadams@work.com"
eaddress(3) = "kjones@work.com"

For i = 1 To 3
    'For Each i In user
        fullName = user(i)
        eName = eaddress(i)
    'Exit For
    debug.print "User is " & fullname & "email to " & eName
Next i


寻找当前用户的 eaddress/eName(用于单独的 Sub to email 文件)。

标签: excelvba

解决方案


您可以使用 aDictionary使这更容易

Private Sub UsereMail()
    Dim dictInfo, fullName

    fullName = Application.UserName

    Set dictInfo = CreateObject("Scripting.Dictionary")
    dictInfo.Add "John Smith", "jsmith@work.com"
    dictInfo.Add "Debbie Adams", "dadams@work.com"
    dictInfo.Add "Karen Jones", "kjones@work.com"

    If dictInfo.Exists(fullName) Then
        Debug.Print "User is " & fullName & " email to " & dictInfo(fullName)
    End If
End Sub

推荐阅读