【问题标题】:Close workbook with Userform使用用户窗体关闭工作簿
【发布时间】:2016-04-19 19:10:59
【问题描述】:

我在 Excel 中创建了一个用户表单,所以当我打开 xlsm 文件时,它只会打开用户表单,而工作簿是隐藏的。

但是当我使用 [X] 按钮关闭用户表单时,我希望它关闭工作簿和用户表单而不保存。

当我现在关闭用户表单并尝试再次打开同一个文件时,它说它已经/仍然打开。

启动代码:

Private Sub Workbook_Open()
 Application.Visible = False
 Fordelinger.Show vbModeless
End Sub

关闭代码:

Private Sub Fordelinger_Deactivate()
Application.Quit
Workbooks("EstimatDOK.xlsm").Close True
End Sub

有人可以帮忙吗? :)

【问题讨论】:

  • 你的代码是否到达Application.Quit之后的行?
  • 我尝试在最后设置 Application.Quit 行,但我仍然遇到同样的问题。
  • 你试过这个解决方案了吗? stackoverflow.com/questions/3628252/…
  • 如果你打开你的任务管理器,发现excel还在打开,你可能要设置excel=0,搜索“为什么excel不会关闭”

标签: vba excel


【解决方案1】:

您可能希望在用户窗体代码窗格中使用此代码

Private Sub UserForm_Terminate()
    ThisWorkbook.Close
End Sub

【讨论】:

  • 你得到你需要的东西了吗?
【解决方案2】:

您可以使用以下代码来限制关闭 (X) 按钮

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
        Cancel = True
        MsgBox "The X is disabled, please use a button on the form to Exit.", vbCritical
    End If
End Sub

Private Sub UserForm_Terminate()
    ThisWorkbook.Close savechanges:=False
    Application.Quit
End Sub

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2019-06-02
    • 2020-02-27
    • 2015-07-24
    相关资源
    最近更新 更多