Userfrom X按钮停止代码

时间:2015-09-03 10:33:44

标签: excel vba excel-vba userform

我需要一些帮助来结束一些代码,我的工作表(NewMonth)上有一个按钮,它调用UserFrom3,然后在填写用户表单并将数据复制到工作表后运行更多代码。

UserForm3是日期选择。填充完成后,用日期填充工作表上的单元格,然后从NewMonth按钮运行其余代码。

我遇到的问题是,如果有人点击用户的X按钮,它仍会运行NewMonth按钮中的代码。

我怎样才能让它停止运行?

以下是NewMonth按钮和userform的代码。

用户窗体:

template <class T>
struct MyInterface
{
  typedef T ParamType;  // Added

  virtual T Foo() = 0;
};


template<class ImplType>
class MyHub
{
    typedef typename ImplType::ParamType T;
public:
    static T Foo()
    {
        ImplType i;
        return i.Foo();
    }

private:
    MyHub() { }
    ~MyHub() { }
};

int main()
{
    int i = MyHub<MyImpl>::Foo();

    return 0;
}

NewMonth Button:

Private Sub btnDateAdd_Click()
Dim rDate As Range, rDate2 As Range
Dim Month As String, Year As String

Month = ComboBox1.Value
Year = TextBox1.Value

Set rDate = Sheets("PAYMENT FORM").Range("C13")
Set rDate2 = Sheets("PAYMENT FORM").Range("L13")

If ComboBox1.Value & TextBox1.Value = "" Then
MsgBox "Please select the Payment Month & Year required!"
Exit Sub
ElseIf ComboBox1.Value = "" Then
MsgBox "Please select the Payment Month required!"
Exit Sub
ElseIf TextBox1.Value = "" Then
MsgBox "Please select the Payment Year required!"
Exit Sub
Else
rDate.Value = Format((28 & " " & Month & " " & Year), "dd mmm yy")
rDate2.Value = Format((28 & " " & Month & " " & Year), "mmmm yyyy")
End If

Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim iCtr As Long
For iCtr = 1 To 12
Me.ComboBox1.AddItem Format(DateSerial(2007, iCtr, 28), "mmmm")
Next iCtr
End Sub

1 个答案:

答案 0 :(得分:0)

我搜索了几个网站并尝试了多个灵魂,我设法让它使用下面的代码。

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
        End
    End If
End Sub