0

Hi I want to send email mail to the address which is checked I have:

  1. Check box
  2. Column Name
  3. Column Email

    Sub reminder1()
    
    Dim lRow As Integer
    Dim i As Integer
    Dim toList As String
    Dim eSubject As String
    Dim eBody As String
    
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With
    
    Sheets(1).Select
    lRow = Cells(Rows.Count, 4).End(xlUp).Row
    
    For i = 2 To lRow
    
        If Sheets("Sheet1").CheckBox1.Value = True Then
    
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)
            Cells(i, 5) = "Mail Sent " & Date + Time
            Cells(i, 5).Font.Bold = True
    
            toList = Cells(i, 3)
    
            eSubject = "Your "
    
            eBody = "Good Day"
    
            On Error Resume Next
    
            With OutMail
                .To = toList
                .CC = ""
                .BCC = ""
                .Subject = eSubject
                .BodyFormat = olFormatHTML
                .Display
                .HTMLBody = eBody & vbCrLf & .HTMLBody
                '.Send
            End With
    
            On Error GoTo 0
            Set OutMail = Nothing
            Set OutApp = Nothing 
        End If
        Next i
    
        ActiveWorkbook.Save
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .DisplayAlerts = True
        End With
    End Sub
    

Problem is that if i check first one it send email to all if not checked it is not sending email even other check boxes are checked

Community
  • 1
  • 1
Kzhel Farmer
  • 112
  • 2
  • 10

2 Answers2

0

You need to loop through the check boxes. Your current code is hardcoded to check only the first check box, which is the "CheckBox1".

Instead of:

If Sheets("Sheet1").CheckBox1.Value = True Then
'code
end if

Use something like this:

If ActiveSheet.OLEObjects("Checkbox"&i-1).Object.Value  = True Then
'code
End If

Alternative Instead of a checkbox, use dropdown with true / false Then use something like this:

if cells(i,1).value = True then
'code
end if
luvlogic
  • 103
  • 2
  • 12
  • i tried to put this ThisWorkbook.Worksheets("Sheet1").Shapes("CheckBox"&i).Value = True Then but getting RUN TIME ERROR 438 Object doesn't support this property or method – Kzhel Farmer May 28 '15 at 09:52
  • 'If ActiveSheet.OLEObjects("Checkbox"&i-1).Object.Value = True Then' its working perfect Thanks – Kzhel Farmer May 28 '15 at 11:15
0

I 'd propose that you loop through all check boxes and try to find the check box which is applicable to the row you are currently on. So, in order to stick with your solution and having a checkbox in each row you need to verify which checkbox applies to the row you are in and see if the checkbox is checked.

Sub reminder1()

Dim lRow As Integer
Dim i As Integer
Dim toList As String
Dim eSubject As String
Dim eBody As String
Dim oleControl As OLEObject

With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
End With

Sheets(1).Select
lRow = Cells(Rows.Count, 4).End(xlUp).Row

For i = 2 To lRow
    For Each oleControl In Sheets("Sheet1").OLEObjects
        If Range(oleControl.TopLeftCell.Address).Row = i Then
            If oleControl.Object.Value = True Then

                Set OutApp = CreateObject("Outlook.Application")
                Set OutMail = OutApp.CreateItem(0)
                Cells(i, 5) = "Mail Sent " & Date + Time
                Cells(i, 5).Font.Bold = True

                toList = Cells(i, 3)

                eSubject = "Your "

                eBody = "Good Day"

                On Error Resume Next

                With OutMail
                    .To = toList
                    .CC = ""
                    .BCC = ""
                    .Subject = eSubject
                    .BodyFormat = olFormatHTML
                    .Display
                    .HTMLBody = eBody & vbCrLf & .HTMLBody
                    '.Send
                End With

                On Error GoTo 0
                Set OutMail = Nothing
                Set OutApp = Nothing

            End If
        End If
    Next oleControl 
Next i

ActiveWorkbook.Save

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True
End With

End Sub

Note that this code assumes that the top left corner of the checkbox is within the row to which the checkbox applies. If that's not the case then you can also use the .BottomRightCell.Address or a mix of the two. Also note that this code does not verify if there are other shapes on the sheet such as combo boxes or buttons or other things.

Ralph
  • 9,284
  • 4
  • 32
  • 42
  • yes check box is in same rows as name and emails but i am getting this RUN TIME ERROR 438 Object doesn't support this property or method on this line """"If shp.ControlFormat.Value = xlOn And Range(shp.TopLeftCell.Address).Row = i Then""" Check box is Cloumn A row 2 to row say 20 and have no other shapes on sheet – Kzhel Farmer May 28 '15 at 10:14
  • @kzhel-farmer: I forgot that you are using ActiveX controls and not form controls. I adjusted the code. So, now it should work. – Ralph May 28 '15 at 10:23
  • got this error now Compile error SUB or Function not defined "OLEObjects" is highlighted and yellow thing is on first line on SUB – Kzhel Farmer May 28 '15 at 10:31
  • @kzhel-farmer: The code works fine here. I just adjusted the code a bit more to go through the `Sheets("Sheet1").OLEObjects`collection instead of `Sheets("Sheet1").Shapes`as before. Maybe that helps. Again this code also works fine on my end. – Ralph May 28 '15 at 10:46
  • Possibly you want to add `If InStr(1, o.Name, "CheckBox") > 0 Then` as offered in [this solution](http://stackoverflow.com/questions/18927684/unselect-all-checkboxes-from-excel-workbook-with-vba-macro) in order to make sure that this really is a checkbox. [This one](http://stackoverflow.com/questions/11991308/how-do-i-use-checkboxes-in-an-if-then-statement-in-excel-vba-2010) might be also relevant if you are partially using ActiveX controls and partially form controls. – Ralph May 28 '15 at 10:48
  • 'Sub reminder1() Sheets(1).Select lRow = Cells(Rows.Count, 4).End(xlUp).Row For i = 2 To lRow For Each shp In Sheets("Sheet1").OLEObjects If shp.Type = 12 And Range(shp.TopLeftCell.Address).Row = i Then If OLEObjects(shp.Name).Object.Value = True Then code here End Sub' i copy paste all this code getting same error compile error just to let you know i have button to run this macro OLEObjects is giving error – Kzhel Farmer May 28 '15 at 10:53
  • it is completely Active X control check box and have one button to run this macro and other cells just manual entry of names and emails – Kzhel Farmer May 28 '15 at 10:58
  • i know it is some small error but finding it hard to figure out – Kzhel Farmer May 28 '15 at 10:59
  • i changed ' If OLEObjects(shp.Name).Object.Value = True Then ' with ' If ActiveSheet.OLEObjects(shp.Name).Object.Value = True Then ' not getting error but it is not sending email not doing anything – Kzhel Farmer May 28 '15 at 11:09