2013-11-22 44 views
0

所以我試圖實現的是非常基本的。基本上,我有一些用戶窗體中的文本框,將值插入到工作表中的表格中。在將這些數值放入該表格之前,有一些基本要求需要實現。下面的代碼應該解釋我正在嘗試做什麼。如果UserForm框爲空,則停止插入表中的數據

我遇到的問題是,當一個文本框是空的,並彈出錯誤消息(根據我的標準),當你點擊確定消息後代碼繼續寫入其餘的數據該工作表減去最初沒有數據的一個UserForm框,然後清除所有表單。我想要做的就是保存數據,當你點擊「確定」時,它會將你帶到需要填寫表格的地方。 (SetFocus的)

Private Sub addItem_Click() 
Dim the_sheet As Worksheet 
Dim table_object_row As ListRow 
Set the_sheet = Sheets("NewOrder") 

'find first empty row in database 
Set table_list_object = the_sheet.ListObjects(1) 
Set table_object_row = table_list_object.ListRows.Add 


'check for a part number 
If Me.taxBox = True Then 
    Tax = "N" 
End If 

If Trim(Me.txtItem.Value) = "" Then 
    Me.txtItem.SetFocus 
    MsgBox "Enter an item" 
     Else 
     If Trim(Me.txtSKU.Value) = "" Then 
      Me.txtSKU.SetFocus 
     MsgBox "Enter SKU" 

      Else 
      If Trim(Me.txtPerc.Value) = "" And Trim(Me.txtAdjust.Value) = "" Then 
      Me.txtPerc.SetFocus 
      MsgBox "Enter percent or adjusted price" 

       Else 
       If Trim(Me.txtPrice.Value) = "" And Trim(Me.txtAdjust.Value) = "" Then 
       Me.txtPrice.SetFocus 
       MsgBox "Enter original price" 
       End If 
      End If 
     End If 
    End If 


If Trim(Me.txtPerc.Value) = "" And Me.txtAdjust.Value > 0 Then 
With table_object_row 
the_sheet.Unprotect Password:="password" 

    .Range(1, 1).Value = Me.txtItem.Value 
    .Range(1, 2).Value = Me.txtSKU.Value 
    .Range(1, 3).Value = Me.txtPrice.Value 
    .Range(1, 4).Value = Me.txtPerc.Value 
    .Range(1, 5).Value = Me.txtAdjust.Value 
    .Range(1, 6).Value = Me.txtQTY.Value 
    .Range(1, 7).Value = Tax 

    the_sheet.Protect Password:="password" 
End With 

'clear the data 
Me.txtItem.Value = "" 
Me.txtSKU.Value = "" 
Me.txtPrice.Value = "" 
Me.txtPerc.Value = "" 
Me.txtAdjust.Value = "" 
Me.txtQTY.Value = "" 





    Else 
    If Trim(Me.txtAdjust.Value) = "" And Me.txtPrice.Value > 0 And Me.txtPerc.Value > 0 Then 

    With table_object_row 
the_sheet.Unprotect Password:="password" 
    .Range(1, 1).Value = Me.txtItem.Value 
    .Range(1, 2).Value = Me.txtSKU.Value 
    .Range(1, 3).Value = Me.txtPrice.Value 
    .Range(1, 4).Value = Me.txtPerc.Value 
    .Range(1, 6).Value = Me.txtQTY.Value 
    .Range(1, 7).Value = Tax 
the_sheet.Protect Password:="password" 
End With 


'clear the data 
Me.txtItem.Value = "" 
Me.txtSKU.Value = "" 
Me.txtPrice.Value = "" 
Me.txtPerc.Value = "" 
Me.txtAdjust.Value = "" 
Me.txtQTY.Value = "" 
Me.txtItem.SetFocus 
End If 
End If 

End Sub 

回答

0

代碼被構造,一旦出錯框ndisplayed和判決同意的方式,代碼繼續處理其它邏輯。

另外,還有一些其他的結構性問題。下面是一個重構,其中包括一些'***評論注意到的變化。

Dim the_sheet As Worksheet 
    Dim table_object_row As ListRow 

'*** Declare all your variables 
    Dim table_list_object As ListObject 
    Dim Tax As Variant 

    Set the_sheet = Sheets("NewOrder") 

    'find first empty row in database 
    Set table_list_object = the_sheet.ListObjects(1) 
    '** move this to after checks 
    'Set table_object_row = table_list_object.ListRows.Add 


    'check for a part number 
    If Me.taxBox = True Then 
     Tax = "N" 
    End If 

    '*** Use If Then ElseIf structure 
    If Trim(Me.txtItem.Value) = "" Then 
     Me.txtItem.SetFocus 
     MsgBox "Enter an item" 
    ElseIf Trim(Me.txtSKU.Value) = "" Then 
     Me.txtSKU.SetFocus 
     MsgBox "Enter SKU" 
    ElseIf Trim(Me.txtPerc.Value) = "" And Trim(Me.txtAdjust.Value) = "" Then 
     Me.txtPerc.SetFocus 
     MsgBox "Enter percent or adjusted price" 
    ElseIf Trim(Me.txtPrice.Value) = "" And Trim(Me.txtAdjust.Value) = "" Then 
     Me.txtPrice.SetFocus 
     MsgBox "Enter original price" 
    Else 
     '*** Only execute this if no blanks are found 
     If Trim(Me.txtPerc.Value) = "" And Me.txtAdjust.Value > 0 Then 
      '*** Move create new row to where you are certain to add data 
      Set table_object_row = table_list_object.ListRows.Add 
      With table_object_row 
       'the_sheet.Unprotect Password:="password" 

       .Range(1, 1).Value = Me.txtItem.Value 
       .Range(1, 2).Value = Me.txtSKU.Value 
       .Range(1, 3).Value = Me.txtPrice.Value 
       .Range(1, 4).Value = Me.txtPerc.Value 
       .Range(1, 5).Value = Me.txtAdjust.Value 
       .Range(1, 6).Value = Me.txtQTY.Value 
       .Range(1, 7).Value = Tax 

       'the_sheet.Protect Password:="password" 
      End With 

      'clear the data 
      Me.txtItem.Value = "" 
      Me.txtSKU.Value = "" 
      Me.txtPrice.Value = "" 
      Me.txtPerc.Value = "" 
      Me.txtAdjust.Value = "" 
      Me.txtQTY.Value = "" 
     ElseIf Trim(Me.txtAdjust.Value) = "" And Me.txtPrice.Value > 0 And Me.txtPerc.Value > 0 Then 
      '*** Move create new row to where you are certain to add data 
      Set table_object_row = table_list_object.ListRows.Add 
      With table_object_row 
       'the_sheet.Unprotect Password:="password" 
       .Range(1, 1).Value = Me.txtItem.Value 
       .Range(1, 2).Value = Me.txtSKU.Value 
       .Range(1, 3).Value = Me.txtPrice.Value 
       .Range(1, 4).Value = Me.txtPerc.Value 
       .Range(1, 6).Value = Me.txtQTY.Value 
       .Range(1, 7).Value = Tax 
       'the_sheet.Protect Password:="password" 
      End With 

      'clear the data 
      Me.txtItem.Value = "" 
      Me.txtSKU.Value = "" 
      Me.txtPrice.Value = "" 
      Me.txtPerc.Value = "" 
      Me.txtAdjust.Value = "" 
      Me.txtQTY.Value = "" 
      Me.txtItem.SetFocus 
     Else 
      '*** what if we reach here? 
      MsgBox "What Now?" 
     End If 
    End If 
End Sub 
0

@Chris Neilsen謝謝代碼運行良好。我確實保護了紙張,所以我必須移動幾行來適應這種情況。在我需要的地方,這是我最終得到的:

the_sheet.Unprotect Password:="password" 
    Set table_object_row = table_list_object.ListRows.Add 
    With table_object_row 
相關問題