簡體   English   中英

VBA用戶窗體僅在單擊命令按鈕后才執行選擇

[英]VBA userform to execute selections only after hitting command button

我很難讓vba僅在按下命令按鈕(在這種情況下為GO按鈕)后才能執行我的選擇。 附帶的是用戶窗體的圖片(不是由activeX控件制作的)以及我正在處理的代碼。 謝謝!

用戶表格圖片

Private Sub MR_Click()

If MR.Value = True Then
Rows(6).Delete
End If

End Sub

Private Sub PMS_Click()

If PMS.Value = True Then
Rows(7).Delete
End If

End Sub

Private Sub VOIDMR_Click()

If VOIDMR.Value = True Then
Rows(13).Delete
End If

End Sub

Private Sub VOIDPMS_Click()

If VOIDPMS.Value = True Then
Rows(14).Delete
End If

End Sub

像這樣,使用“轉到”按鈕的_Click事件過程(大概是GO_Click() ,但需要進行修改)來檢查每個復選框並相應地進行刪除。

Private Sub GO_Click()
If MR.Value = True Then
    Rows(6).Delete
End If
If PMS.Value = True Then
    Rows(7).Delete
End If
If VOIDMR.Value = True Then
    Rows(13).Delete
End If
If VOIDPMS.Value = True Then
    Rows(14).Delete
End If
End Sub
'These event procedures won't do anything, and can be removed:
Private Sub MR_Click()
End Sub
Private Sub PMS_Click()
End Sub
Private Sub VOIDMR_Click()
End Sub
Private Sub VOIDPMS_Click()
End Sub

不再需要復選框的_Click事件過程。

要注意事件過程,它們將在事件發生時觸發。 因此,對於帶有_Click事件的復選框,用戶_Click檢查(或取消_Click_Click處理程序都將執行。

如前所述,從集合中刪除元素時要格外小心,因為這通常需要以相反的順序進行(當刪除第6行時,第7行變為第6行,因此,如果同時選中了MR和PMS,則第7行中的內容最初會不能刪除)。 這可能需要進行一些其他代碼更改,但很簡單,似乎只需對GO_Click的命令重新排序GO_Click

Private Sub GO_Click()
If VOIDPMS.Value = True Then
    Rows(14).Delete
End If
If VOIDMR.Value = True Then
    Rows(13).Delete
End If
If PMS.Value = True Then
    Rows(7).Delete
End If
If MR.Value = True Then
    Rows(6).Delete
End If

End Sub

只是@DavidZemens代碼的風格變化:

Private Sub GO_Click()
    With Me '<-- reference the userform
        Select Case True
            Case .MR '<-- if MR checkbox control is True
                Rows(6).Delete
            Case .PMS '<-- if PMS checkbox control is True
                Rows(7).Delete
            Case .VOIDMR '<-- if VOIDMR checkbox control is True
                Rows(13).Delete
            Case .VOIDPMS '<-- if VOIPMS checkbox control is True
                Rows(14).Delete
        End Select
    End With
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM