2013-04-17 106 views
0

我在Excel中使用VB創建了一個請求表單,該表單使用提交按鈕在Outlook中根據輸入到表單中的值生成電子郵件。Excel VBA - 根據另一個字段的值創建多個字段

,一切工作正常。但是,用戶在提交請求之前往往無法完成所有必要的字段。

我需要確保用戶完成所有必需的字段時,進入小區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 

方法2:

當提交按鈕被按下...

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

這裏有一條路要走一下:

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 
+1

我認爲這些應該是'在#1:99-99之間OR's。對於#2,它不應該是「<5」嗎? –

+0

doh!你很對!我會馬上糾正它...... – sous2817

+0

對於2號來說是+1,我認爲這是要走的路。在#1上,我會再做一件事 - 圍繞'ORs'放置括號。如果沒有他們,我不確定它會起作用,無論如何,他們會澄清這些情況。 –

相關問題