[英]Excel VBA UserForm 'OK'
有誰知道如何使用戶窗體功能與消息框的“確定”按鈕相同? 我會解釋。
我在電子表格的一列中檢測到錯誤。 發現錯誤時,將彈出一個消息框,如下所示:
MsgBox“請輸入有效數據”
當我選擇“確定”時,它將轉到該列中的下一個錯誤。 這很棒,當然除了消息框是模式的,它凍結了應用程序。 我希望用戶能夠編輯數據,然后移至下一個錯誤。 因此,我設計了一個用戶表單,該表單可以是非模式的。 很好,除了我希望宏前進到下一個錯誤。 如果用戶糾正了錯誤,它將執行該操作。 如果沒有,則僅停留在該錯誤單元中。
我知道為什么會這樣。 我的userform“下一步”按鈕僅調用查找第一個錯誤的宏。 但是我想知道的是,是否有辦法解決這個問題。
錯誤檢查從第19行開始,因為這是用戶輸入數據的開始位置。
我在此處包含指向電子表格的鏈接。 模塊1“ NextValidationError”的工作原理很好,並繼續處理下一個錯誤。 模塊14只是在錯誤處掛起,直到解決為止。 我希望它能夠跳過。
誰能給我關於如何使模塊14像模塊1一樣進行的建議?
像這樣:
Dim r_start As Long
Sub CheckNames()
Dim r As Long
'Dim emptyRow As Boolean
If r_start = 0 Then r_start = 19
With ActiveSheet
For r = r_start To 5000
'Checks entire row for data. User may skip rows when entering data.
If WorksheetFunction.CountA(.Range(.Cells(r, 1), .Cells(r, 33))) > 0 Then
If ((.Cells(r, 2) = "") <> (.Cells(r, 3) = "")) Or _
((.Cells(r, 2) = "") = (.Cells(r, 4) = "")) Then
MsgBox "Please fill in First and Last Name or HCO in Row " & r & "."
End If
End If
Next
End With
End Sub
除非我誤讀了您的代碼,否則可以將兩個檢查與Or
結合使用。
當用戶完成檢查后(如果此后表單保持打開狀態),您將需要一些方法來重置r_start
。
編輯:這是一個非常基本的示例。 UserForm1有兩個按鈕-“下一步”和“關閉”
“ next”的代碼僅是:
Private Sub CommandButton1_Click()
ShowErrors
End Sub
在常規模塊中:
Dim r_start As Long
'this kicks off the checking process
Sub StartChecking()
r_start = 0
UserForm1.Show vbModeless
ShowErrors
End Sub
'a simple example validation...
Sub ShowErrors()
Dim c As Range, r As Long
If r_start = 0 Then r_start = 9
For r = r_start To 200
With ActiveSheet.Rows(r)
If Not IsNumeric(.Cells(1).Value) Then
UserForm1.lblMsg.Caption = "Cell " & .Cells(1).Address() & " is not numeric!"
r_start = r + 1
Exit Sub
End If
End With
Next r
r_start = 0
UserForm1.lblMsg.Caption = "No more errors"
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.