Match username (array) to email (array)

php array
php array length
php implode
php foreach
php array count values multidimensional array
php array push
php count array elements with specific value
php print array

Using Excel 2013. My first post after years of finding and adapting.

I'm trying to match the current App user, ie "John Smith", to his email address "jsmith@work.com".

Using two strings: 1 for users (1 to 3), the other eaddress(1 To 3). I want to get the current user's email address for use in separate Sub that CCs the current user on an email.

I tried a For Each i In user, and set eName to eaddress(i). This returned only the last listed user/email.

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


Looking to get the eaddress/eName of the current user (for use in separate Sub to email file).

You can use a Dictionary to make this easier

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

preg_replace - Manual, Searches subject for matches to pattern and replaces them with replacement . If this parameter is a string and the pattern parameter is an array, all patterns will You may or should also put a log-message or the sending of an email into the� get-filename is a custom function that launches a file browser so the user has a gui to select the input file. stores the path of the input file into gfn_file.

Using your original array-based approach:

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

array_count_values - Manual, array_count_values() returns an array using the values of array as keys and their frequency in array as values. sergolucky96 at gmail dot com � I couldn't find a function for counting the values with case-insensitive matching, so I wrote a� The preg_match() function searches a string for pattern, returning true if the pattern exists, and false otherwise.

Another dictionary based approach where the list of users does not have to be in the same order as the email addresses.

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

PHP Array Functions, array_uintersect_uassoc(), Compare arrays, and returns the matches (compare keys and values, using two user-defined key comparison functions). Is there a way I can search a multdimensional array to match a username and id on the same line and return true/false? For example like what I’ve wrote below, except the if/else function is

How to order WP_User_Query results to match the order of an array , Updated: The WP_User_Query class, in WordPress 4.1+, supports it with : ' orderby' => 'include'. Since WordPress 4.7 there's also support for: 'orderby'� In Excel, arrays in worksheets are limited by available random access memory, by the total number of array formulas, and by the "entire column" rule. Available memory The Excel versions that are listed in the "Applies to" section do not impose a limit on the size of worksheet arrays.

$all — MongoDB Manual, When passed an array of a nested array (e.g. [ [ "A" ] ] ), $all matches documents where the field contains the nested array as an element (e.g. field: [ [ "A" ], . It is used in online application form or social sites to signup account to verify the entered password by user is correct or not. It is the simple method to verify the password matches. First password is stored into a password1 variable and confirm password is stored in password2 variable. Then check if both variable value is equal then

get_users() | Function, get_users( array $args = array() ). Retrieve list of users matching criteria. An array is a consecutive group of memory locations that are of the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. The illustration given below shows an integer array called C that contains 11 elements.

Comments
  • need to move your debug.print inside your loop.
  • @tigeravatar Ok, moved d.p inside, and it correctly matches FOR EACH user/address. I need to extract just 1 Match. I think i need an IF user(i) Then eName = eaddress, but i'm not getting it right.
  • Are you hard coding these username/addresses, or are they being pulled from a location in the file? your code indicates these are hard coded, but that may be just for the example.
  • @Cyril Names and emails are hardcoded. May eventually move to a hidden sheet in the workbook, but for now...
  • I feel like the answers are good enough it doesn't warrant mine variant. But in a more robust project in the future, you might want to consider creating your own class of 'User' and create a Collection of users.
  • Thanks @pankaj Jaju! Works like a charm. Just have to add all my users...but this is easy! (Sorry, i'm too low a rep for my vote to count!)
  • Thanks @Tim Williams! with the error catch is also great!