简体   繁体   English

VBA用户窗体仅在单击命令按钮后才执行选择

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

I am having a hard time to make vba to execute my selection only after pressing the command button (The GO button in this case). 我很难让vba仅在按下命令按钮(在这种情况下为GO按钮)后才能执行我的选择。 Attached are the picture of the userform, which is not made by activeX control, and the code I am working on. 附带的是用户窗体的图片(不是由activeX控件制作的)以及我正在处理的代码。 Thanks! 谢谢!

用户表格图片

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

Like this, using a "Go" button's _Click event procedure (presumably GO_Click() , but modify if needed) to check against each of the checkboxes and do the deletion accordingly. 像这样,使用“转到”按钮的_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

The checkboxes' _Click event procedures are no longer needed. 不再需要复选框的_Click事件过程。

To note about the event procedures, they will fire whenever the event occurs. 要注意事件过程,它们将在事件发生时触发。 So for an checkbox with a _Click event, any time the user checks (or unchecks) _Click handler will execute. 因此,对于带有_Click事件的复选框,用户_Click检查(或取消_Click_Click处理程序都将执行。

As noted, exercise caution when deleting elements from a collection, as this typically needs to be done in reverse order (when you delete row 6, row 7 becomes row 6, so if both MR and PMS are checked, what's in Row 7 initially will not be deleted). 如前所述,从集合中删除元素时要格外小心,因为这通常需要以相反的顺序进行(当删除第6行时,第7行变为第6行,因此,如果同时选中了MR和PMS,则第7行中的内容最初会不能删除)。 This may require some additional code change, but very simply it seems by just re-ordering the commands within GO_Click , should handle this: 这可能需要进行一些其他代码更改,但很简单,似乎只需对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

just a stylistic variation on @DavidZemens code: 只是@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