I have a list of usernames in a spreadsheet and I would like to simply check whether or not the username exists within a VBA script in Excel. In this case the username is what we use to login to our Company's computers. In this case a letter followed by 6 numbers.

I started the following sub (just trying to get one to work before I code the looping) along with the function I'm trying to use, but have trouble knowing what to pass for the objUser. The code description said to use "objUser.Get(“distinguishedName”)" but I don't know what to do with that. For the objRootOU I pass the result of GetObject("LDAP://rootDSE") based on some internet searching of similar scripts.

Thanks for any help anyone can provide

Public Sub CheckUsernames()

Dim oRoot As IADs
Dim oUser As IADs
Dim loginname As String

On Error GoTo ErrHandler:

loginname = Sheets("Sheet1").Range("A1").Value

Set oRoot = GetObject("LDAP://rootDSE")
oUser = oRoot.Get("distinguishedName")

loginname = SearchForUser(loginname, oRoot, oUser)

ErrHandler:
On Error Resume Next

Set oRoot = Nothing
Set oUser = Nothing

End Sub

Private Function SearchForUser(ByVal sLogin, ByRef objRootOU, ByRef objUser)
Dim boolFound
Dim objOU
Dim sOUName, sUserName

boolFound = False
sLogin = UCase(sLogin)

objRootOU.Filter = Array("organizationalUnit")
For Each objOU In objRootOU
    sOUName = Mid(objOU.Name, 4)
    objOU.Filter = Array("user")
    For Each objUser In objOU
        sUserName = Mid(objUser.Name, 4)
        If UCase(sUserName) = sLogin Then
            boolFound = True
            Exit For
        End If
    Next
   
    If boolFound Then
        Exit For
    End If
Next

SearchForUser = boolFound

End Function