jeudi 29 août 2019

Excel VBA check multiple checkboxes

I have 2 checkboxes within my Excel sheet; option1 and option2. I am wishing to run a macro to check if option1 is selected, option2 is selected, or neither of them is selected.

Once I have checked if the checkboxes are selected I will then do the following:

  • 'option1' - Dispay message relating to option 1

  • 'option2' - Display message relating to option 2

  • neither selected - display a message that neither was selected

These will then be sent out as an email with the text corresponding to option 1 or option 2.

-

-

Here is an attempt of code I made, but not complete

If Sheet1.CheckBox1.Value = True Then

SEND OPTION1 RELATED CONTENT

    ElseIf
    Sheet1.CheckBox2.Value = True Then

SEND OPTION2 RELATED CONTENT

Else **neither option1 or option2 selected --not sure on this**
    Sub MsgBoxCriticalIcon()
    MsgBox "You must select an option", vbCritical
    End Sub
End If

Here is my working code without my attempts inserted..

Sub Email_VBA()


    Dim OlApp As Object
    Dim NewMail As Object
    Dim TempFilePath As String
    Dim FileExt As String
    Dim TempFileName As String
    Dim FileFullPath As String
    Dim MyWb As Workbook


    Set MyWb = ThisWorkbook

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

    'Save your workbook in your temp folder of your system
    'below code gets the full path of the temporary folder
    'in your system

    TempFilePath = Environ$("temp") & "\"
    'Now get the extension of the file
    'below line will return the extension
    'of the file
    FileExt = "." & LCase(Right(MyWb.Name, Len(MyWb.Name) - InStrRev(MyWb.Name, ".", , 1)))
    'Now append a date and time stamp
    'in your new file

    TempFileName = MyWb.Name & "-" & Format(Now, "dd-mmm-yy h-mm-ss")

    'Complete path of the file where it is saved
    FileFullPath = TempFilePath & TempFileName & FileExt

    'Now save your currect workbook at the above path
    MyWb.SaveCopyAs FileFullPath

    'Now open a new mail

    Set OlApp = CreateObject("Outlook.Application")
    Set NewMail = OlApp.CreateItem(0)




    On Error Resume Next
    With NewMail
        .To = "ashley@hotmail.com"
        .CC = ""
        .BCC = ""
        .Subject = "NEW Type your Subject here"
        .Body = "NEW Type the Body of your mail"
        .Attachments.Add FileFullPath '--- full path of the temp file where it is saved
        .Display   'or use .Display to show you the email before sending it.
    End With
    On Error GoTo 0

    'Since mail has been sent with the attachment
    'Now delete the temp file from the temp folder

    Kill FileFullPath

    'set nothing to the objects created
    Set NewMail = Nothing
    Set OlApp = Nothing

    'Now set the application properties back to true
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With


End Sub

The end result being both checkboxes are checked and a message being sent in Outlook related to the option chosen. Or if neither are chosen the user is prompted to choose an option.

Feel free to ask any questions & thanks for your help

Kind regards

Aucun commentaire:

Enregistrer un commentaire