我的 vba(IF 陳述句部分)代碼中有一個不匹配的型別,但我想不通,有人可以解釋一下嗎?
在下面找到相關的 vba 代碼。
Private Sub continue_entry_Click()
Dim row_entitee As Long
row_entitee = ThisWorkbook.Sheets("TABLE_ENTITEE").Range("A" & Rows.Count).End(xlUp).Row 1
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("A" & row_entitee) = manual_entry.zone_entry.Value
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("B" & row_entitee) = manual_entry.country_entry.Value
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("C" & row_entitee) = manual_entry.entity_entry.Value
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("D" & row_entitee) = manual_entry.code_entry.Value
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("E" & row_entitee) = manual_entry.activity_entry.Value
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("F" & row_entitee) = manual_entry.currency_entry.Value
entity_selection.entity_list.Value = manual_entry.entity_entry.Value
If ThisWorkbook.Sheets("TABLE_ENTITEE").Range("A" & row_entitee).Value And _
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("B" & row_entitee).Value And _
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("C" & row_entitee).Value And _
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("D" & row_entitee).Value And _
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("E" & row_entitee).Value And _
ThisWorkbook.Sheets("TABLE_ENTITEE").Range("F" & row_entitee).Value Is Empty _
Then MsgBox "All the field must be filled" Else _
manual_entry.Hide: entity_selection.Show
End Sub
感謝您的幫助,并祝您有美好的一天。
呼吸
我試圖讓我的用戶表單的每個欄位都被填寫,否則會出現一個 msgbox。
uj5u.com熱心網友回復:
我認為 FreeFlow 已經確定了您收到錯誤訊息的直接原因。我添加了該建議并使用物件變數簡化了您的代碼以參考您的作業表并給出您的“IF”陳述句標準格式。
Private Sub continue_entry_Click()
Dim row_entitee As Long
Dim s As Worksheet
Set s = ThisWorkbook.Sheets("TABLE_ENTITEE")
row_entitee = s.Range("A" & Rows.Count).End(xlUp).Row 1
s.Cells(row_entitee, "A") = manual_entry.zone_entry.Value
s.Cells(row_entitee, "B") = manual_entry.country_entry.Value
s.Cells(row_entitee, "C") = manual_entry.entity_entry.Value
s.Cells(row_entitee, "D") = manual_entry.code_entry.Value
s.Cells(row_entitee, "E") = manual_entry.activity_entry.Value
s.Cells(row_entitee, "F") = manual_entry.currency_entry.Value
entity_selection.entity_list.Value = manual_entry.entity_entry.Value
If s.Cells(row_entitee, "A").Value And _
s.Cells(row_entitee, "B").Value And _
s.Cells(row_entitee, "C").Value And _
s.Cells(row_entitee, "D").Value And _
s.Cells(row_entitee, "E").Value And _
IsEmpty(s.Cells(row_entitee, "F").Value) Then
MsgBox "All the fields must be filled"
Else
manual_entry.Hide
entity_selection.Show
End If
End Sub
uj5u.com熱心網友回復:
在這個答案中,我將向您展示如何在表單上進行資料驗證。在寫入作業表之前,我會檢查表單中的值。只有通過所有資料驗證檢查,我才會寫入作業表。
在這種方法中,我從一個名為“message”的變數作為空字串(所有字串變數的起始值)開始。然后我檢查表格上的每個條目,看看是否輸入了某些內容。如果沒有,我附加到訊息變數。
在所有資料驗證檢查之后,如果訊息變數仍然為空,則我處理資料,否則,我使用訊息變數向用戶顯示問題所在。此代碼假定您正在檢查的表單上的控制元件是文本框、組合框或串列框。
Private Sub continue_entry_Click()
Dim row_entitee As Long
Dim s As Worksheet
Dim message As String
'data validation checks
If Len(manual_entry.zone_entry.Value) = 0 Then message = message & ", zone"
If Len(manual_entry.country_entry.Value) = 0 Then message = message & ", country"
If Len(manual_entry.entity_entry.Value) = 0 Then message = message & ", entity"
If Len(manual_entry.zone_entry.code_entry) = 0 Then message = message & ", code"
If Len(manual_entry.activity_entry.Value) = 0 Then message = message & ", activity"
If Len(manual_entry.currency_entry.Value) = 0 Then message = message & ", currency"
If Len(message) = 0 Then
' all data validation checks pass, the message is still an empty string,
' we are ready to proceed
Set s = ThisWorkbook.Sheets("TABLE_ENTITEE")
row_entitee = s.Range("A" & Rows.Count).End(xlUp).Row 1
s.Cells(row_entitee, "A") = manual_entry.zone_entry.Value
s.Cells(row_entitee, "B") = manual_entry.country_entry.Value
s.Cells(row_entitee, "C") = manual_entry.entity_entry.Value
s.Cells(row_entitee, "D") = manual_entry.code_entry.Value
s.Cells(row_entitee, "E") = manual_entry.activity_entry.Value
s.Cells(row_entitee, "F") = manual_entry.currency_entry.Value
entity_selection.entity_list.Value = manual_entry.entity_entry.Value
manual_entry.Hide
entity_selection.Show
Else
' at least one one data validation rule failed
message = message "All fields are required, please check the following:" & Mid(message, 2)
MsgBox message, vbCritical
End If
End Sub
我沒有執行此代碼,因為我沒有您參考的表單,因此可能需要進行一些除錯;-)
uj5u.com熱心網友回復:
(a)如前所述,檢查的語法必須是IsEmpty(cell). 如果你使用cell is Empty,你會得到一個錯誤 424 (object required)
(b)您的型別不匹配來自If Cells(row_entitee, "A").Value And...- 這將強制 VBA 將單元格的內容轉換為布林值。這對于字串值將失敗(并為數值提供不需要的結果)。
(c)我假設您想在任何值為空時發出警告。在這種情況下,您需要使用Or而不是And.
(d)恕我直言,最好在將輸入寫回作業表之前檢查輸入,但這是您需要做出的決定。
(e)你對 If - Else - 的用法很奇怪。不要連接 theIf和Else-branch 的陳述句并將它們分開:(我的建議:根本不要使用:)
Private Sub continue_entry_Click()
Dim ws As Worksheet, row_entitee As Long
Set ws = ThisWorkbook.Sheets("TABLE_ENTITEE")
row_entitee = ws.Range("A" & ws.Rows.Count).End(xlUp).row 1
ws.Range("A" & row_entitee) = manual_entry.zone_entry.Value
ws.Range("B" & row_entitee) = manual_entry.country_entry.Value
ws.Range("C" & row_entitee) = manual_entry.entity_entry.Value
ws.Range("D" & row_entitee) = manual_entry.code_entry.Value
ws.Range("E" & row_entitee) = manual_entry.activity_entry.Value
ws.Range("F" & row_entitee) = manual_entry.currency_entry.Value
entity_selection.entity_list.Value = manual_entry.entity_entry.Value
If IsEmpty(ws.Range("A" & row_entitee).Value) _
Or IsEmpty(ws.Range("B" & row_entitee).Value) _
Or IsEmpty(ws.Range("C" & row_entitee).Value) _
Or IsEmpty(ws.Range("D" & row_entitee).Value) _
Or IsEmpty(ws.Range("E" & row_entitee).Value) _
Or IsEmpty(ws.Range("F" & row_entitee).Value) Then
MsgBox "All the field must be filled"
Else
manual_entry.Hide
entity_selection.Show
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/451401.html
