[英]Excel VBA check multiple checkboxes
I have 2 checkboxes within my Excel sheet; 我的Excel工作表中有2个复选框; option1 and option2.
选项1和选项2。 I am wishing to run a macro to check if option1 is selected, option2 is selected, or neither of them is selected.
我希望运行一个宏,以检查是否选择了option1,是否选择了option2,或者均未选择。
Once I have checked if the checkboxes are selected I will then do the following: 检查完复选框后,我将执行以下操作:
'option1' - Dispay message relating to option 1 'option1'-与选项1有关的付款消息
'option2' - Display message relating to option 2 'option2'-显示有关选项2的消息
These will then be sent out as an email with the text corresponding to option 1 or option 2. 然后将这些作为电子邮件发送出去,其中包含与选项1或选项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. 选中了两个复选框的最终结果,并在Outlook中发送了与所选选项相关的消息。 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 亲切的问候
Here is your structure to check for the selected options: 这是检查所选选项的结构:
Sub Checkboxes()
If Sheet1.CheckBox1.Value = True And Sheet1.CheckBox2.Value = True Then
Call Bothtrue
Else
If Sheet1.CheckBox1.Value = True Then
Call Only1True
Else
If Sheet1.CheckBox2.Value = True Then
Call Only2True
Else
Call MsgBoxCriticalIcon
Exit Sub
End If
End If
End If
End Sub
Then for every option you call a new sub with the different info like so: 然后,对于每个选项,请使用不同的信息调用一个新的子项,如下所示:
Sub Only1True()
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
End With
End Sub
And for the last option you need this: 对于最后一个选项,您需要这样做:
Sub MsgBoxCriticalIcon()
MsgBox "You must select an option", vbCritical
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.