I have a custom excel function `GetADUser' which takes a Username as input returns several Active Directory Attributes likes Firstname, Surname, SAM Account Name, Distinguished Name.
How can I get these attributes into cells to the left and right of the cell which holds the forumla. ie:
Public Function GetADUser(UserName As String) As String
Dim mycell As Range
Set rootDSE = GetObject("LDAP://RootDSE")
Base = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"
'filter on user objects with the given account name
fltr = "(&(objectClass=user)(objectCategory=Person)" & _
"(sAMAccountName=" & UserName & "))"
'add other attributes according to your requirements
attr = "distinguishedName,sn,mobile,sAMAccountName,GivenName,l,postOfficeBox"
Scope = "subtree"
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "Active Directory Provider"
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = Base & ";" & fltr & ";" & attr & ";" & Scope
Set rs = cmd.Execute
arrPOBox = rs.Fields("postOfficeBox").Value
Rank = CStr(arrPOBox(0))
ActiveCell.Offset(0, -1).Value = (rs.Fields("sn").Value)
ActiveCell.Offset(0, -2).Value = (rs.Fields("GivenName").Value)
ActiveCell.Offset(0, 2).Value = (rs.Fields("l").Value)
ActiveCell.Offset(0, 1).Value = (rs.Fields("mobile").Value)
rs.Close
conn.Close
GetADUser = GetADUser
End Function
However ActiveCell is not available in Functions.
I did read a way to return a variant instead of String, but it involved CTRL-SHIFT-ENTER to split out the values, which all went to the right of the cell holding the formula. I don't want to make a call to Active Directory for each cell.
Is there a function or procedure that could be implemented such that when a user exits a cell in the username column the other relative cells are populated.
UPDATE
This should have been detailed in the original question, but the user name cells could be in any sheet in the Workbook, and not a continuous set of cells in one of four possible columns. (see yellow cells for example)
The sheet names could also be changed.
The Intersect method has a limit (30) to the ranges it can take.
I considered regex since the username is always [a-z]{4}[a-z]{2} but then it triggers on every cell.
How would I do the intersect?