【问题标题】:Match username (array) to email (array)将用户名(数组)与电子邮件(数组)匹配
【发布时间】:2019-04-24 03:08:15
【问题描述】:

使用 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


希望获取当前用户的电子地址/电子名称(用于单独的 Sub 到电子邮件文件)。

【问题讨论】:

  • 需要将您的debug.print 移动到您的循环中。
  • @tigeravatar 好的,将 d.p 移到里面,它正确匹配每个用户/地址。我只需要提取 1 个匹配项。我想我需要一个 IF 用户(i)然后 eName = eaddress,但我没有做对。
  • 您是对这些用户名/地址进行硬编码,还是从文件中的某个位置提取它们?您的代码表明这些是硬编码的,但这可能只是示例。
  • @Cyril 姓名和电子邮件是硬编码的。最终可能会移动到工作簿中的隐藏工作表,但现在......
  • 我觉得答案已经足够好了,它不保证我的变体。但在未来更强大的项目中,您可能需要考虑创建自己的“用户”类并创建用户集合。

标签: excel vba


【解决方案1】:

您可以使用Dictionary 来简化此操作

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

【讨论】:

  • 谢谢@pankaj Jaju!奇迹般有效。只需添加我所有的用户......但这很容易! (对不起,我的代表太低了,我的投票数无法计算!)
【解决方案2】:

使用您原来的基于数组的方法:

Sub tester()
    Debug.Print "John Smith", UserEmail("John Smith") '>> jsmith@work.com
    Debug.Print "John Brown", UserEmail("John Brown") '>> [blank]
End Sub


Private Function UserEmail(userName As String) As String

    Dim user(1 To 3), eaddress(1 To 3), m

    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"

    m = Application.Match(userName, user, 0)
    If Not IsError(m) Then UserEmail = eaddress(m)

End Function

【讨论】:

  • 感谢@Tim Williams!错误捕获也很棒!
【解决方案3】:

另一种基于字典的方法,其中用户列表不必与电子邮件地址的顺序相同。

Option Explicit

Sub Test()
Dim user(1 To 3)            As String
Dim eaddress(1 To 3)        As String
Dim user_dic                As Scripting.Dictionary

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"

Set user_dic = MatchUsersToEmail(eaddress, user)

Debug.Print eaddress(1), user_dic.Item(eaddress(1))
Debug.Print eaddress(2), user_dic.Item(eaddress(2))
Debug.Print eaddress(3), user_dic.Item(eaddress(3))

End Sub

Public Function MatchUsersToEmail(ByRef email_array() As String, ByRef user_array() As String) As Scripting.Dictionary
' Returns a scripting dictionary where the email address returns the user name
Dim my_users                As Scripting.Dictionary
Dim my_user                 As Variant
Dim my_email                As Variant
Dim my_name()               As String
Dim my_key                  As String
    Set my_users = New Scripting.Dictionary

    For Each my_email In email_array
        ' Add the email address as the key
        my_users.Add Key:=CStr(my_email), Item:=vbNullString

    Next

    For Each my_user In user_array
        my_name = Split(LCase$(my_user))
        my_key = Left$(my_name(0), 1) & my_name(1) & "@work.com"
        my_users.Item(my_key) = my_user

    Next

    Set MatchUsersToEmail = my_users

End Function

【讨论】:

    猜你喜欢
    • 2014-05-18
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多