Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-根据另一个字段的值设置多个必填字段_Excel_Vba - Fatal编程技术网

Excel VBA-根据另一个字段的值设置多个必填字段

Excel VBA-根据另一个字段的值设置多个必填字段,excel,vba,Excel,Vba,我用VB在Excel中创建了一个请求表单,它使用提交按钮根据表单中输入的值在outlook中生成电子邮件 一切正常。但是,在提交请求之前,用户通常无法完成所有必需的字段 在提交请求之前,我需要确保用户在单元格D7中输入特定值时完成所有必填字段 这就是我迷路的地方…我试着用两种稍微不同的方式来接近它 希望有人能帮我 方法1: 当按下提交按钮时 Button_click() If Range("D7").Value = "Special Request" THEN 'Make cells B6,

我用VB在Excel中创建了一个请求表单,它使用提交按钮根据表单中输入的值在outlook中生成电子邮件

一切正常。但是,在提交请求之前,用户通常无法完成所有必需的字段

在提交请求之前,我需要确保用户在单元格D7中输入特定值时完成所有必填字段

这就是我迷路的地方…我试着用两种稍微不同的方式来接近它

希望有人能帮我

方法1:

当按下提交按钮时

Button_click()

If Range("D7").Value = "Special Request" THEN
'Make cells B6, B7, B8, B9, D14 mandatory in order to generate email 

    On Error Resume Next
    If ThisWorkbook.Worksheets("Request").Range _
    ("B6, B7, B8, B9, D14 ") Is Nothing Then

    MsgBox ("Please confirm all required fields have been completed!")

'Do not generate email
Button_click()


'If the value of cell D7 is ANYTHING OTHER THAN "Special Feature", 
'execute code as normal to generate email

'Else, check for empty fields in the required cells ("B6, B7, B8, B9, D14 ") 

'if any required cells are empty, display message and do not generate email    
MsgBox ("Please confirm all required fields have been completed!")   

'If D7 = "Special Feature" AND NO REQUIRED FIELDS ARE MISSING, 
'continue with executing code to     generate email

方法2:

当按下提交按钮时

Button_click()

If Range("D7").Value = "Special Request" THEN
'Make cells B6, B7, B8, B9, D14 mandatory in order to generate email 

    On Error Resume Next
    If ThisWorkbook.Worksheets("Request").Range _
    ("B6, B7, B8, B9, D14 ") Is Nothing Then

    MsgBox ("Please confirm all required fields have been completed!")

'Do not generate email
Button_click()


'If the value of cell D7 is ANYTHING OTHER THAN "Special Feature", 
'execute code as normal to generate email

'Else, check for empty fields in the required cells ("B6, B7, B8, B9, D14 ") 

'if any required cells are empty, display message and do not generate email    
MsgBox ("Please confirm all required fields have been completed!")   

'If D7 = "Special Feature" AND NO REQUIRED FIELDS ARE MISSING, 
'continue with executing code to     generate email

这里有一种方法:

    Sub test()
If Range("D7").Value = "Special Request" And _
    Range("B6").Value = "" Or _
    Range("B7").Value = ""  Or _
    Range("B8").Value = "" Or _
    Range("B9").Value = "" Or _
    Range("D14").Value = "" Then
        MsgBox ("Please confirm all required fields have been completed!")
        Exit Sub
Else
        ' whatever method you're using to generate email goes here

End If



End Sub
下面是使用CountA的另一种方法:

Sub test2()

If Range("D7").Value = "Special Request" And _ 
Application.WorksheetFunction.CountA(Range("B6:B9"), Range("D14")) < 5 Then
        MsgBox ("Please confirm all required fields have been completed!")
        Exit Sub
Else
        ' whatever method you're using to generate email goes here

End If


End Sub
子测试2()
如果范围(“D7”).Value=“特殊要求”和
Application.WorksheetFunction.CountA(范围(“B6:B9”)、范围(“D14”))小于5
MsgBox(“请确认所有必填字段均已填写!”)
出口接头
其他的
“无论你用什么方法来生成电子邮件,都可以在这里找到
如果结束
端接头

我认为这些应该在#1中的范围内
s。对于#2,我认为应该是第2位的
+1。在#1上,我还要做一件事——在
OR
周围加上括号。我不确定没有他们它是否能工作,而且无论如何,他们会澄清条件。值得一提的是,在我的模型上,#1在没有任何括号的情况下工作。我也喜欢#2,但我想有选择是很好的。#1有一些好处,比如,如果业务需要更改,您可以为每个字段指定各种条件,而无需进行大量返工,尽管如果您对大量必填字段(其中唯一的要求是“不能为空”)过于疯狂,那么效率会有所下降。太棒了!使用第一种方法,效果很好。感谢所有的投入!非常感谢!