2015-05-29 67 views
0

我希望當ComboBox2等於NiPd時會出現一個彈出框,那麼ComboBox6的值必須落在我設置的範圍內。
如果超出範圍,將顯示彈出消息。
除此之外,當代碼值超過3.2時,我的代碼也會檢查TextBox8值,它也會顯示彈出消息。數據將不會存儲在excel表格中

但是我已經編譯了所有的代碼,當textbox8小於3.2時,當commandbutton1被點擊時,它不能將數據存儲在Excel表格中。

除此之外,我還發現如果我選擇在TextBox8中重新鍵入值,它會自動存儲我不希望發生的數據。 我希望,它只會存儲我重新輸入的數據。 我堅持這一點,我完全不知道我的代碼裏面有什麼問題。

我的代碼如下:

Private Sub CommandButton1_Click() 

Sheets("Overall").Activate 

Dim m As Variant, RequiredRange As Variant 
Dim msg As Integer 

RequiredRange1 = Array("30S", "30A", "40S") 
RequiredRange2 = Array("10A", "15S", "15A", "20S") 
RequiredRange3 = Array("30S", "30A", "40S") 

If Me.ComboBox2.Value = "NiPd" Then 
m = Application.Match(ComboBox6.Value, RequiredRange1, False) 
If IsError(m) Then 

msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _ 
"Selection Value Out Of Range" & Chr(10) & Chr(10) & _ 
"Do You Want To Continue With Submission?", 36, "Warning") 

If msg = 7 Then Me.ComboBox6.SetFocus: Exit Sub 
End If 
End If 


If Me.ComboBox2.Value = "NiAu" Then 

m = Application.Match(ComboBox6.Value, RequiredRange2, False) 
If IsError(m) Then 

msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _ 
"Selection Value Out Of Range" & Chr(10) & Chr(10) & _ 
"Do You Want To Continue With Submission?", 36, "Warning") 

If msg = 7 Then Me.ComboBox6.SetFocus: Exit Sub 
End If 
End If 

If Me.ComboBox2.Value = "NiPdAu" Then 

m = Application.Match(ComboBox6.Value, RequiredRange3, False) 
If IsError(m) Then 

msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _ 
"Selection Value Out Of Range" & Chr(10) & Chr(10) & _ 
"Do You Want To Continue With Submission?", 36, "Warning") 

If msg = 7 Then Me.ComboBox6.SetFocus: Exit Sub 
End If 
End If 

With Me 
If Len(.ComboBox1.Value) * Len(.TextBox1.Value) * Len(.ComboBox7.Value) * Len(.ComboBox3.Value) * Len(.ComboBox2.Value) * Len(.TextBox2.Value) * Len(.TextBox3.Value) * Len(.ComboBox4.Value) * Len(.ComboBox5.Value) * Len(.TextBox4.Value) * Len(.TextBox5.Value) * Len(.TextBox6.Value) * Len(.ComboBox6.Value) * Len(.TextBox7.Value) * Len(.TextBox8.Value) * Len(.TextBox9.Value) = 0 Then 
    MsgBox "Please Complete All Fields Before Submit" 
Else 

    If CSng(.TextBox8.Text) > 3 Then 
     If MsgBox("Plating Rate below than 3.0 um, Kindly stop production and use another Ni Bath" & vbLf & vbLf & _ 
        "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then 

      MsgBox "user to re-type the value in TextBox8.", vbInformation, "Warning" 

    If CSng(.TextBox8.Text) = 3.2 Then 
     If MsgBox("Plating Rate below than 3.2 um , Standby the next Ni bath and start heat up to 65°" & vbLf & vbLf & _ 
        "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then 

      MsgBox "user to re-type the value in TextBox8.", vbInformation, "Warning" 


      Exit Sub 
     End If 
        End If 
    End If 

    eRow = Sheet4.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 
    Cells(eRow, 2).Value = ComboBox1.Text 
    Cells(eRow, 5).Value = TextBox1.Text 
    Cells(eRow, 1).Value = ComboBox7.Text 
    Cells(eRow, 6).Value = ComboBox3.Text 
    Cells(eRow, 15).Value = ComboBox2.Text 
    Cells(eRow, 17).Value = TextBox2.Text 
    Cells(eRow, 18).Value = TextBox3.Text 
    Cells(eRow, 9).Value = ComboBox4.Text 
    Cells(eRow, 11).Value = ComboBox5.Text 
    Cells(eRow, 7).Value = TextBox4.Text 
    Cells(eRow, 8).Value = TextBox5.Text 
    Cells(eRow, 14).Value = TextBox6.Text 
    Cells(eRow, 16).Value = ComboBox6.Text 
    Cells(eRow, 12).Value = TextBox7.Text 
    Cells(eRow, 13).Value = TextBox8.Text 
    Cells(eRow, 19).Value = TextBox9.Text 
    End If 
End If 
End With 
End Sub 
+1

歡迎來到[SO](http://stackoverflow.com/tour)。你已經傾倒了94行代碼。 ([見](http://stackoverflow.com/help/mcve)) – Jeeped

回答

1

你應該避免像If msg = 7 Then Me.ComboBox6.SetFocus: Exit Sub寫代碼,因爲它太難看發生了什麼。相反,分裂的代碼行:

If msg = 7 Then 
    Me.ComboBox6.SetFocus 
    Exit Sub 
End If 

你也應該總是縮進一個如果... END裏面的代碼塊。如果因爲這使得它更容易看到的代碼已經被分組。

如果您重新組織您的代碼,如下所示,查看分組更容易。您還可以使用其他過程來分隔大量代碼。我已經添加了UpdateTheSheet proc。

Private Sub CommandButton1_Click() 

    Sheets("Overall").Activate 

    Dim m As Variant, RequiredRange As Variant 
    Dim msg As Integer 

    RequiredRange1 = Array("30S", "30A", "40S") 
    RequiredRange2 = Array("10A", "15S", "15A", "20S") 
    RequiredRange3 = Array("30S", "30A", "40S") 

    If Me.ComboBox2.Value = "NiPd" Then 
     m = Application.Match(ComboBox6.Value, RequiredRange1, False) 
     If IsError(m) Then 
      msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _ 
      "Selection Value Out Of Range" & Chr(10) & Chr(10) & _ 
      "Do You Want To Continue With Submission?", 36, "Warning") 
      If msg = 7 Then 
       Me.ComboBox6.SetFocus 
       Exit Sub 
      End If 
     End If 
    End If 

    If Me.ComboBox2.Value = "NiAu" Then 
     m = Application.Match(ComboBox6.Value, RequiredRange2, False) 
     If IsError(m) Then 
      msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _ 
       "Selection Value Out Of Range" & Chr(10) & Chr(10) & _ 
       "Do You Want To Continue With Submission?", 36, "Warning") 

      If msg = 7 Then 
       Me.ComboBox6.SetFocus 
       Exit Sub 
      End If 
     End If 
    End If 

    If Me.ComboBox2.Value = "NiPdAu" Then 
     m = Application.Match(ComboBox6.Value, RequiredRange3, False) 
     If IsError(m) Then 
      msg = MsgBox("Stabilizer Reading:" & ComboBox6.Value & Chr(10) & _ 
       "Selection Value Out Of Range" & Chr(10) & Chr(10) & _ 
       "Do You Want To Continue With Submission?", 36, "Warning") 
      If msg = 7 Then 
       Me.ComboBox6.SetFocus 
       Exit Sub 
      End If 
     End If 
    End If 

    With Me 
     If Len(.ComboBox1.Value) * Len(.TextBox1.Value) * Len(.ComboBox7.Value) * Len(.ComboBox3.Value) * Len(.ComboBox2.Value) * Len(.TextBox2.Value) * Len(.TextBox3.Value) * Len(.ComboBox4.Value) * Len(.ComboBox5.Value) * Len(.TextBox4.Value) * Len(.TextBox5.Value) * Len(.TextBox6.Value) * Len(.ComboBox6.Value) * Len(.TextBox7.Value) * Len(.TextBox8.Value) * Len(.TextBox9.Value) = 0 Then 
      MsgBox "Please Complete All Fields Before Submit" 
     Else 
      If CSng(.TextBox8.Text) > 3 Then 
       If MsgBox("Plating Rate below than 3.0 um, Kindly stop production and use another Ni Bath" & vbLf & vbLf & _ 
          "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then 

        MsgBox "user to re-type the value in TextBox8.", vbInformation, "Warning" 

        If CSng(.TextBox8.Text) = 3.2 Then 
         If MsgBox("Plating Rate below than 3.2 um , Standby the next Ni bath and start heat up to 65°" & vbLf & vbLf & _ 
            "Do you wish to continue?", vbYesNo, "Exceeds") = vbNo Then 

          MsgBox "user to re-type the value in TextBox8.", vbInformation, "Warning" 
          Exit Sub 
         End If 
        End If 
       End If 

       eRow = Sheet4.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 
       UpdateTheSheet 
      End If 
     End If 
    End With 
End Sub 

Private Sub UpdateTheSheet() 
    ' Writes the values from user input to the worksheet 
    Cells(eRow, 2).Value = ComboBox1.Text 
    Cells(eRow, 5).Value = TextBox1.Text 
    Cells(eRow, 1).Value = ComboBox7.Text 
    Cells(eRow, 6).Value = ComboBox3.Text 
    Cells(eRow, 15).Value = ComboBox2.Text 
    Cells(eRow, 17).Value = TextBox2.Text 
    Cells(eRow, 18).Value = TextBox3.Text 
    Cells(eRow, 9).Value = ComboBox4.Text 
    Cells(eRow, 11).Value = ComboBox5.Text 
    Cells(eRow, 7).Value = TextBox4.Text 
    Cells(eRow, 8).Value = TextBox5.Text 
    Cells(eRow, 14).Value = TextBox6.Text 
    Cells(eRow, 16).Value = ComboBox6.Text 
    Cells(eRow, 12).Value = TextBox7.Text 
    Cells(eRow, 13).Value = TextBox8.Text 
    Cells(eRow, 19).Value = TextBox9.Text 
End Sub 
相關問題