Excel VBA选中多个复选框

Excel VBA选中多个复选框,excel,vba,if-statement,checkbox,Excel,Vba,If Statement,Checkbox,我的Excel表格中有2个复选框;选项1和选项2。我希望运行宏以检查是否选择了选项1、选项2或两者均未选择 选中复选框后,我将执行以下操作: “选项1”-与选项1相关的显示消息 “选项2”-显示与选项2相关的信息 均未选中-显示均未选中的消息 然后,这些信息将以电子邮件的形式发送出去,其中包含与选项1或选项2相对应的文本 - - 这里是我尝试编写的代码,但不完整 If Sheet1.CheckBox1.Value = True Then SEND OPTION1 RELATED CONTE

我的Excel表格中有2个复选框;选项1和选项2。我希望运行宏以检查是否选择了选项1、选项2或两者均未选择

选中复选框后,我将执行以下操作:

  • “选项1”-与选项1相关的显示消息

  • “选项2”-显示与选项2相关的信息

  • 均未选中-显示均未选中的消息
然后,这些信息将以电子邮件的形式发送出去,其中包含与选项1或选项2相对应的文本

-

-

这里是我尝试编写的代码,但不完整

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
这是我的工作代码,没有插入我的尝试

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
最终结果是,选中两个复选框,并在Outlook中发送与所选选项相关的消息。或者,如果两者都未选择,则会提示用户选择一个选项

欢迎提出任何问题&谢谢你的帮助


亲切问候

以下是检查所选选项的结构:

子复选框()
如果Sheet1.CheckBox1.Value=True和Sheet1.CheckBox2.Value=True,则
叫博思图
其他的
如果Sheet1.CheckBox1.Value=True,则
仅呼叫1对
其他的
如果Sheet1.CheckBox2.Value=True,则
仅呼叫2对
其他的
调用MsgBoxCriticalIcon
出口接头
如果结束
如果结束
如果结束
端接头
然后,对于每个选项,您调用具有不同信息的新sub,如下所示:

Sub only 1true()
与NewMail
.To=”ashley@hotmail.com"
.CC=“”
.BCC=“”
.Subject=“在此处键入您的主题”
.Body=“新建邮件正文”
.Attachments.Add FileFullPath'---保存临时文件的完整路径
.展示
以
端接头
对于最后一个选项,您需要:

子MsgBoxCriticalIcon()
MsgBox“您必须选择一个选项”,vbCritical
端接头

我将在NewMail.To=”中添加该
ashley@hotmail.com“.CC=”“.BCC=”“.Subject=”在此处键入您的主题“.Body=”新建邮件正文”.Attachments.Add FileFullPath'---保存临时文件的完整路径。Display'或使用.Display在发送电子邮件之前向您显示电子邮件。以
结束时,内容将根据选项1或选项2的选择而更改编辑似乎需要4个结果,具体取决于复选框的选择。您的工作进展顺利,但您的逻辑流程有点偏离。使用
从选中的两个选项开始。然后使用您拥有的两个选项,然后以您所述的关键文本框结束。