所以我試圖實現的是非常基本的。基本上,我有一些用戶窗體中的文本框,將值插入到工作表中的表格中。在將這些數值放入該表格之前,有一些基本要求需要實現。下面的代碼應該解釋我正在嘗試做什麼。如果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