Can For Each change cell contents without any code to do so?

savindrasingh

Board Regular
Joined
Sep 10, 2009
Messages
183
I am facing a strange problem with For Each Loop. While debugging my code I have found that as soon as I enter the line highlighted below (having For Each Associate In empList) it is changing the cell contents with user's display name without even running the code? It is so strange, I am not able to figure-out how this is happening.. I am using F8 to try step by step debugging and as soon as I hit F8 at above mentioned line, the contents of cell A2 changes. Please suggest how to fix this.
Code:
Option Explicit
Public Enum MessageType
    Birthday
    Anniversary
End Enum
Public BCCList
Sub AutoMailer()
Application.OnTime TimeValue("09:00:00"), Code.TrackEvents
End Sub
Sub TrackEvents()
'*** Prevent repeated run by checking value in this cell ***'
Dim userResponse
If ThisWorkbook.Sheets("Details").Range("G1").Value = Date Then
    If MsgBox("The emails were already sent for today." & vbCrLf & _
        "Do you still want to re-run?", vbYesNo, "Auto-wish-mailer") = vbYes Then
        '*** Continue with the code ***'
    Else
        'ThisWorkbook.Close SaveChanges:=True
        Exit Sub
    End If
End If
'*** Employee list from this column using email Alias/login ID ***'
Dim empList, Associate As Range
Set empList = ThisWorkbook.Sheets("Details").Range("A2:A" & _
    ThisWorkbook.Sheets("Details").Range("A" & ThisWorkbook.Sheets("Details").Rows.Count).End(xlUp).Row)
'*** Other variables required for the code ***'
Dim MonthOfJoining, MonthOfBirth, DOJ, DOB, CurrentDay, CurrentMonth, YearsCompleted, TimeSpan, PersonNumber
'*** Determine todays day and month for considering wishes ***'
CurrentDay = Day(Date): CurrentMonth = Month(Date)
[U][B]For Each Associate In empList
[/B][/U]    If UserExists(Associate, Associate) Then
        Debug.Print Associate.Offset(0, 3).Value
        If Associate.Offset(0, 3).Value = "Mithun" Then
            Debug.Assert Associate.Offset(0, 3).Value = "Mithun"
        End If
        '*** Values for Joining Anniversary ***'
        If Associate.Offset(0, 1).Value = "?" Or Associate.Offset(0, 1).Value = "" Then
            '** Do Nothing as no data available for this associate ***'
        Else
            '** Determine Month and Day values **'
            MonthOfJoining = Month(Associate.Offset(0, 1).Value): DOJ = Day(Associate.Offset(0, 1).Value)
            MonthOfBirth = Month(Associate.Offset(0, 2).Value): DOB = Day(Associate.Offset(0, 2).Value)
            CurrentDay = Day(Date): CurrentMonth = Month(Date)
            
            '** Determine # of years for joining anniversary **'
            TimeSpan = Year(Date) - Year(Associate.Offset(0, 1).Value)
            
            If TimeSpan > 1 Then
                YearsCompleted = TimeSpan & " Glorious years "
            Else
                YearsCompleted = TimeSpan & " year "
            End If
            
            '** Send emails to team members having birthdays today **'
            If MonthOfBirth = CurrentMonth And DOB = CurrentDay Then
                SendEmail Associate.Offset(0, 3).Value, Associate.Value, Birthday
            End If
            
            '** Send emails to team members having Anniversaries today **'
            If TimeSpan >= 1 Then
                If MonthOfJoining = CurrentMonth And DOJ = CurrentDay Then
                    SendEmail Associate.Offset(0, 3).Value, Associate.Value, Anniversary, YearsCompleted
                End If
            End If
        End If
    End If
Next
'** Lock repeat execution due to accidental opening of workbook by setting value in this cell **'
ThisWorkbook.Sheets("Details").Range("G1").Value = Date
'ThisWorkbook.Close SaveChanges:=True
End Sub
Sub SendEmail(ByVal EmpName As String, ByVal e_Mail As String, ByVal WishType As MessageType, Optional ByVal Period As String)
Dim olApp, olMail, MessageText, objFSO, strImage
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Select Case WishType
    Case MessageType.Birthday
        olMail.Attachments.Add (ThisWorkbook.Path & "\" & "bday.jpg")
        olMail.Subject = "May your birthday be filled with excitement, joy, and laughter"
        olMail.HTMLBody = [HTML]"            "background='cid:bday.jpg'> 


" & _
            "Dear " & EmpName & "

We wish you a very special Birthday today
" & _
            "Your birthday is a special time to celebrate the gift of ‘you’ to the world...

" & _
            "Happy Birthday !!
" & _
            "" & _
            "Regards,
EUC Family
"[/HTML]
    Case MessageType.Anniversary
        olMail.Attachments.Add (ThisWorkbook.Path & "\" & "anniv.jpg")
        olMail.Subject = "Congratulations!!! On Completing " & Period & "at Wells Fargo"
        olMail.HTMLBody = "[HTML]            "background='cid:anniv.jpg'>



" & _
            "Dear " & EmpName & "

Congratulations to you for completing " & Period & " and achieving a
" & _
            "Milestone in your career with us. 
" & _
            "We recognize your contribution to the organization.

" & _
            "" & _
            "Regards,
EUC Family
"[/HTML]
    Case Else
        olMail.Subject = "Test mail"
        olMail.HTMLBody = [HTML]"Hello " & EmpName & ",

This is test email. Please ignore."[/HTML]
End Select
olMail.To = e_Mail
'olMail.SentonBehalfOfName = "[EMAIL="dis.tea.events@xyz.com"]dis.tea.events@xyz.com[/EMAIL]"
Code.GetBCC
olMail.BCC = BCCList & "; GPlatformEngineering; EUCCoreSupportTeam"
'*** Change from olMail.Send to olMail.Display to toggle between sending or just displaying email ***'
olMail.Display
Set objFSO = Nothing
Set olApp = Nothing
Set olMail = Nothing
End Sub
Sub GetBCC()
BCCList = ""
Dim BCCRange, BCCItem
Set BCCRange = Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row)
For Each BCCItem In BCCRange
    If BCCList <> "" Then
        BCCList = BCCList & "; " & BCCItem
    Else
        BCCList = BCCItem
    End If
Next
End Sub
Function UserName()
UserName = UCase(Environ("UserName"))
End Function
Function UserExists(sUser, sDisName)
  Dim oConn, oCMD, oRoot, sDNSDomain, sQuery, sFilter, oResults
  UserExists = False
  sDisName = sUser
  On Error Resume Next
  ' Use ADO to search the domain for all users.
  Set oConn = CreateObject("ADODB.Connection")
  Set oCMD = CreateObject("ADODB.Command")
  oConn.Provider = "ADsDSOOBject"
  oConn.Open "Active Directory Provider"
  Set oCMD.ActiveConnection = oConn
  ' Determine the DNS domain from the RootDSE object.
  Set oRoot = GetObject("[URL]ldap://RootDSE[/URL]")
  sDNSDomain = oRoot.Get("DefaultNamingContext")
  sFilter = "(&(ObjectClass=user)(ObjectCategory=person)(samAccountName=" & sUser & "))"
  sQuery = "<ldap: "="" &="" sdnsdomain="">;" & sFilter & ";displayName;subtree"
  oCMD.CommandText = sQuery
  oCMD.Properties("Page Size") = 100
  oCMD.Properties("Timeout") = 30
  oCMD.Properties("Cache Results") = False
  Set oResults = oCMD.Execute
  Do Until oResults.EOF
    If oResults.Fields("displayName") <> "" Then
      sDisName = oResults.Fields("displayName")
      UserExists = True
    End If
    oResults.MoveNext
  Loop
  On Error GoTo 0
End Function
</ldap:>
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I am using F8 to try step by step debugging and as soon as I hit F8 at above mentioned line, the contents of cell A2 changes.
What does A2 change to? Has it continued to do this between restarts? It's a shot in the dark but I've seen strange behavior in VBA that went away after restarting.

To answer your main question, no, to my knowledge the first line of a For Each loop shouldn't do anything like you're describing.
 
Upvote 0
Also, I see you're passing ranges to the UserExists function by reference. So, your UserExists function could be changing the contents of the cells. Are you sure that's not where the change is occurring?
 
Upvote 0
Well, as mentioned in the question, " it is changing the cell contents with user's display name without even running the code". However, the function itself does not have any code to replace the contents of any Cell. It returns either true/false.
 
Upvote 0
I just changed the function name from UserExists to CheckIfUserExists. Mysteriously, that solved the issue. I don't want this issue to get appended to the list of unsolved mysteries of my career. Can someone please try and report if they are facing the same issues with this code?
Prerequisites:
1. Should have Active directory environment
2. Should have Outlook installed and configured
3. Column A consists of Domain user IDs followed by (imaginary) Date of joining, Date of Birth, First Name and email address in Column B, C, D and E respectively. I will post the code in next reply immediately.
 
Upvote 0
I just changed the function name from UserExists to CheckIfUserExists. Mysteriously, that solved the issue. I don't want this issue to get appended to the list of unsolved mysteries of my career. Can someone please try and report if they are facing the same issues with this code?
Prerequisites:
1. Should have Active directory environment
2. Should have Outlook installed and configured
3. Column A consists of Domain user IDs followed by (imaginary) Date of joining, Date of Birth, First Name and email address in Column B, C, D and E respectively. I will post the code in next reply immediately.
Code:
Option Explicit
Public Enum MessageType
    Birthday
    Anniversary
End Enum
Public BCCList, DisplayNameOfUser
Sub AutoMailer()
Application.OnTime TimeValue("09:00:00"), Code.TrackEvents
End Sub
Sub TrackEvents()
'*** Prevent repeated run by checking value in this cell ***'
Dim userResponse
If ThisWorkbook.Sheets("Details").Range("G1").Value = Date Then
    If MsgBox("The emails were already sent for today." & vbCrLf & _
        "Do you still want to re-run?", vbYesNo, "Auto-wish-mailer") = vbYes Then
        '*** Continue with the code ***'
    Else
        'ThisWorkbook.Close SaveChanges:=True
        Exit Sub
    End If
End If
'*** Employee list from this column using email Alias/login ID ***'
Dim empList, Associate As Range
Set empList = ThisWorkbook.Sheets("Details").Range("A2:A" & _
    ThisWorkbook.Sheets("Details").Range("A" & ThisWorkbook.Sheets("Details").Rows.Count).End(xlUp).Row)
'*** Other variables required for the code ***'
Dim MonthOfJoining, MonthOfBirth, DOJ, DOB, CurrentDay, CurrentMonth, YearsCompleted, TimeSpan, PersonNumber
'*** Determine todays day and month for considering wishes ***'
CurrentDay = Day(Date): CurrentMonth = Month(Date)
For Each Associate In empList
    If CheckIfUserExists(Associate.Value, "") Then
        If UCase(Left(DisplayNameOfUser, 3)) <> "ZZ." Then
            Debug.Print Associate.Offset(0, 3).Value
            If Associate.Offset(0, 3).Value = "Mithun" Then
                Debug.Assert Associate.Offset(0, 3).Value = "Mithun"
            End If
            '*** Values for Joining Anniversary ***'
            If Associate.Offset(0, 1).Value = "?" Or Associate.Offset(0, 1).Value = "" Then
                '** Do Nothing as no data available for this associate ***'
            Else
                '** Determine Month and Day values **'
                MonthOfJoining = Month(Associate.Offset(0, 1).Value): DOJ = Day(Associate.Offset(0, 1).Value)
                MonthOfBirth = Month(Associate.Offset(0, 2).Value): DOB = Day(Associate.Offset(0, 2).Value)
                CurrentDay = Day(Date): CurrentMonth = Month(Date)
                
                '** Determine # of years for joining anniversary **'
                TimeSpan = Year(Date) - Year(Associate.Offset(0, 1).Value)
                
                If TimeSpan > 1 Then
                    YearsCompleted = TimeSpan & " Glorious years "
                Else
                    YearsCompleted = TimeSpan & " year "
                End If
                
                '** Send emails to team members having birthdays today **'
                If MonthOfBirth = CurrentMonth And DOB = CurrentDay Then
                    SendEmail Associate.Offset(0, 3).Value, Associate.Value, Birthday
                End If
                
                '** Send emails to team members having Anniversaries today **'
                If TimeSpan >= 1 Then
                    If MonthOfJoining = CurrentMonth And DOJ = CurrentDay Then
                        SendEmail Associate.Offset(0, 3).Value, Associate.Value, Anniversary, YearsCompleted
                    End If
                End If
            End If
        End If
    End If
Next
'** Lock repeat execution due to accidental opening of workbook by setting value in this cell **'
ThisWorkbook.Sheets("Details").Range("G1").Value = Date
'ThisWorkbook.Close SaveChanges:=True
End Sub
Sub SendEmail(ByVal EmpName As String, ByVal e_Mail As String, ByVal WishType As MessageType, Optional ByVal Period As String)
Dim olApp, olMail, MessageText, objFSO, strImage
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Select Case WishType
    Case MessageType.Birthday
        olMail.Attachments.Add (ThisWorkbook.Path & "\" & "bday.jpg")
        olMail.Subject = "May your birthday be filled with excitement, joy, and laughter"
        olMail.HTMLBody = [HTML]"<html><body style='font-family: Segoe Script; font-size:18pt; color:navy'" & _
            "background='cid:bday.jpg'><font color='navy' size=18pt> <br><br><br>" & _
            "<center>Dear " & EmpName & "<br><br>We wish you a very special Birthday today<br>" & _
            "Your birthday is a special time to celebrate the gift of ‘you’ to the world...<br><br>" & _
            "Happy Birthday !!<br></font>" & _
            "<pre style='font-family: Segoe Script; font-size:16pt; color:navy'>" & _
            "Regards,<br>EUC Family<br></font></pre></center></body></html>"[/HTML]
    Case MessageType.Anniversary
        olMail.Attachments.Add (ThisWorkbook.Path & "\" & "anniv.jpg")
        olMail.Subject = "Congratulations!!! On Completing " & Period & "at Wells Fargo"
        olMail.HTMLBody = [HTML]"<html><body style='font-family: Segoe Script; font-size:18pt; color:navy'" & _
            "background='cid:anniv.jpg'><font color='navy' size=18pt><br><br><br><br>" & _
            "<center>Dear " & EmpName & "<br><br>Congratulations to you for completing <b>" & Period & "</b> and achieving a<br>" & _
            "Milestone in your career with us. <br>" & _
            "We recognize your contribution to the organization.<br><br></font>" & _
            "<pre style='font-family: Segoe Script; font-size:16pt; color:navy'>" & _
            "Regards,<br>EUC Family<br></font></pre></center></body></html>"[/HTML]
    Case Else
        olMail.Subject = "Test mail"
        olMail.HTMLBody = "Hello " & EmpName & ",<br><br>This is test email. Please ignore."
End Select
olMail.To = e_Mail
'olMail.SentonBehalfOfName = "[EMAIL="dis.tea.events@xyz.com"]dis.tea.events@xyz.com[/EMAIL]"
Code.GetBCC
olMail.BCC = BCCList & "; GPlatformEngineering; EUCCoreSupportTeam"
'*** Change from olMail.Send to olMail.Display to toggle between sending or just displaying email ***'
olMail.Display
Set objFSO = Nothing
Set olApp = Nothing
Set olMail = Nothing
End Sub
Sub GetBCC()
BCCList = ""
Dim BCCRange, BCCItem
Set BCCRange = Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row)
For Each BCCItem In BCCRange
    If BCCList <> "" Then
        BCCList = BCCList & "; " & BCCItem
    Else
        BCCList = BCCItem
    End If
Next
End Sub
Function UserName()
UserName = UCase(Environ("UserName"))
End Function
Function CheckIfUserExists(sUser, sDisName)
  Dim oConn, oCMD, oRoot, sDNSDomain, sQuery, sFilter, oResults
  CheckIfUserExists = False
  sDisName = sUser
  On Error Resume Next
  ' Use ADO to search the domain for all users.
  Set oConn = CreateObject("ADODB.Connection")
  Set oCMD = CreateObject("ADODB.Command")
  oConn.Provider = "ADsDSOOBject"
  oConn.Open "Active Directory Provider"
  Set oCMD.ActiveConnection = oConn
  ' Determine the DNS domain from the RootDSE object.
  Set oRoot = GetObject("[URL]ldap://RootDSE[/URL]")
  sDNSDomain = oRoot.Get("DefaultNamingContext")
  sFilter = "(&(ObjectClass=user)(ObjectCategory=person)(samAccountName=" & sUser & "))"
  sQuery = "<LDAP://" & sDNSDomain & ">;" & sFilter & ";displayName;subtree"
  oCMD.CommandText = sQuery
  oCMD.Properties("Page Size") = 100
  oCMD.Properties("Timeout") = 30
  oCMD.Properties("Cache Results") = False
  Set oResults = oCMD.Execute
  Do Until oResults.EOF
    If oResults.Fields("displayName") <> "" Then
      sDisName = oResults.Fields("displayName")
      DisplayNameOfUser = sDisName
      CheckIfUserExists = True
    End If
    oResults.MoveNext
  Loop
  On Error GoTo 0
End Function
 
Upvote 0
Well, as mentioned in the question...
Sorry, I missed that.

However, the function itself does not have any code to replace the contents of any Cell.
This is not true, the line of code in red will change the contents of a cell.
Code:
Option Explicit
Public Enum MessageType
    Birthday
    Anniversary
End Enum
Public BCCList
Sub AutoMailer()
Application.OnTime TimeValue("09:00:00"), Code.TrackEvents
End Sub
Sub TrackEvents()
'*** Prevent repeated run by checking value in this cell ***'
Dim userResponse
If ThisWorkbook.Sheets("Details").Range("G1").Value = Date Then
    If MsgBox("The emails were already sent for today." & vbCrLf & _
        "Do you still want to re-run?", vbYesNo, "Auto-wish-mailer") = vbYes Then
        '*** Continue with the code ***'
    Else
        'ThisWorkbook.Close SaveChanges:=True
        Exit Sub
    End If
End If
'*** Employee list from this column using email Alias/login ID ***'
Dim empList, Associate As Range
Set empList = ThisWorkbook.Sheets("Details").Range("A2:A" & _
    ThisWorkbook.Sheets("Details").Range("A" & ThisWorkbook.Sheets("Details").Rows.Count).End(xlUp).Row)
'*** Other variables required for the code ***'
Dim MonthOfJoining, MonthOfBirth, DOJ, DOB, CurrentDay, CurrentMonth, YearsCompleted, TimeSpan, PersonNumber
'*** Determine todays day and month for considering wishes ***'
CurrentDay = Day(Date): CurrentMonth = Month(Date)
[U][B]For Each Associate In empList
[/B][/U]   If UserExists(Associate, Associate) Then
        Debug.Print Associate.Offset(0, 3).Value
        If Associate.Offset(0, 3).Value = "Mithun" Then
            Debug.Assert Associate.Offset(0, 3).Value = "Mithun"
        End If
        '*** Values for Joining Anniversary ***'
        If Associate.Offset(0, 1).Value = "?" Or Associate.Offset(0, 1).Value = "" Then
            '** Do Nothing as no data available for this associate ***'
        Else
            '** Determine Month and Day values **'
            MonthOfJoining = Month(Associate.Offset(0, 1).Value): DOJ = Day(Associate.Offset(0, 1).Value)
            MonthOfBirth = Month(Associate.Offset(0, 2).Value): DOB = Day(Associate.Offset(0, 2).Value)
            CurrentDay = Day(Date): CurrentMonth = Month(Date)
            
            '** Determine # of years for joining anniversary **'
            TimeSpan = Year(Date) - Year(Associate.Offset(0, 1).Value)
            
            If TimeSpan > 1 Then
                YearsCompleted = TimeSpan & " Glorious years "
            Else
                YearsCompleted = TimeSpan & " year "
            End If
            
            '** Send emails to team members having birthdays today **'
            If MonthOfBirth = CurrentMonth And DOB = CurrentDay Then
                SendEmail Associate.Offset(0, 3).Value, Associate.Value, Birthday
            End If
            
            '** Send emails to team members having Anniversaries today **'
            If TimeSpan >= 1 Then
                If MonthOfJoining = CurrentMonth And DOJ = CurrentDay Then
                    SendEmail Associate.Offset(0, 3).Value, Associate.Value, Anniversary, YearsCompleted
                End If
            End If
        End If
    End If
Next
'** Lock repeat execution due to accidental opening of workbook by setting value in this cell **'
ThisWorkbook.Sheets("Details").Range("G1").Value = Date
'ThisWorkbook.Close SaveChanges:=True
End Sub
Sub SendEmail(ByVal EmpName As String, ByVal e_Mail As String, ByVal WishType As MessageType, Optional ByVal Period As String)
Dim olApp, olMail, MessageText, objFSO, strImage
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Select Case WishType
    Case MessageType.Birthday
        olMail.Attachments.Add (ThisWorkbook.Path & "\" & "bday.jpg")
        olMail.Subject = "May your birthday be filled with excitement, joy, and laughter"
        olMail.HTMLBody = [HTML]"            "background='cid:bday.jpg'> 


" & _
            "Dear " & EmpName & "

We wish you a very special Birthday today
" & _
            "Your birthday is a special time to celebrate the gift of ‘you’ to the world...

" & _
            "Happy Birthday !!
" & _
            "" & _
            "Regards,
EUC Family
"[/HTML]
    Case MessageType.Anniversary
        olMail.Attachments.Add (ThisWorkbook.Path & "\" & "anniv.jpg")
        olMail.Subject = "Congratulations!!! On Completing " & Period & "at Wells Fargo"
        olMail.HTMLBody = "[HTML]            "background='cid:anniv.jpg'>



" & _
            "Dear " & EmpName & "

Congratulations to you for completing " & Period & " and achieving a
" & _
            "Milestone in your career with us. 
" & _
            "We recognize your contribution to the organization.

" & _
            "" & _
            "Regards,
EUC Family
"[/HTML]
    Case Else
        olMail.Subject = "Test mail"
        olMail.HTMLBody = [HTML]"Hello " & EmpName & ",

This is test email. Please ignore."[/HTML]
End Select
olMail.To = e_Mail
'olMail.SentonBehalfOfName = "[EMAIL="dis.tea.events@xyz.com"]dis.tea.events@xyz.com[/EMAIL]"
Code.GetBCC
olMail.BCC = BCCList & "; GPlatformEngineering; EUCCoreSupportTeam"
'*** Change from olMail.Send to olMail.Display to toggle between sending or just displaying email ***'
olMail.Display
Set objFSO = Nothing
Set olApp = Nothing
Set olMail = Nothing
End Sub
Sub GetBCC()
BCCList = ""
Dim BCCRange, BCCItem
Set BCCRange = Range("E2:E" & Range("E" & Rows.Count).End(xlUp).Row)
For Each BCCItem In BCCRange
    If BCCList <> "" Then
        BCCList = BCCList & "; " & BCCItem
    Else
        BCCList = BCCItem
    End If
Next
End Sub
Function UserName()
UserName = UCase(Environ("UserName"))
End Function
Function UserExists(sUser, sDisName)
  Dim oConn, oCMD, oRoot, sDNSDomain, sQuery, sFilter, oResults
  UserExists = False
  sDisName = sUser
  On Error Resume Next
  ' Use ADO to search the domain for all users.
  Set oConn = CreateObject("ADODB.Connection")
  Set oCMD = CreateObject("ADODB.Command")
  oConn.Provider = "ADsDSOOBject"
  oConn.Open "Active Directory Provider"
  Set oCMD.ActiveConnection = oConn
  ' Determine the DNS domain from the RootDSE object.
  Set oRoot = GetObject("[URL]ldap://RootDSE[/URL]")
  sDNSDomain = oRoot.Get("DefaultNamingContext")
  sFilter = "(&(ObjectClass=user)(ObjectCategory=person)(samAccountName=" & sUser & "))"
  sQuery = "<ldap: "="" &="" sdnsdomain="">;" & sFilter & ";displayName;subtree"
  oCMD.CommandText = sQuery
  oCMD.Properties("Page Size") = 100
  oCMD.Properties("Timeout") = 30
  oCMD.Properties("Cache Results") = False
  Set oResults = oCMD.Execute
  Do Until oResults.EOF
    If oResults.Fields("displayName") <> "" Then
[COLOR=#FF0000]      sDisName = oResults.Fields("displayName")[/COLOR]
      UserExists = True
    End If
    oResults.MoveNext
  Loop
  On Error GoTo 0
End Function
</ldap:>

I just changed the function name from UserExists to CheckIfUserExists. Mysteriously, that solved the issue.
You also changed the arguments that you were passing to the function, which is what fixed the problem.<ldap: "="" &="" sdnsdomain="">
</ldap:>
 
Upvote 0

Forum statistics

Threads
1,215,353
Messages
6,124,464
Members
449,163
Latest member
kshealy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top