vba 怎么结束运行中的进程

vba 怎么结束运行中的进程,第1张

---------------------

Public x As String '申明一个公共变量

Sub A()

End Sub

---------------------------------

Private Sub CommandButton1_Click()'  窗体1控件程序代码

'

'你的过程

'

Dim m%

UserForm2.Show

'

'

If x = "CommandButton2" Then

MsgBox x

Exit Sub

End If

If x = "CommandButton1" Then

MsgBox x

End If

'你的过程'

'

End Sub

----------------

Private Sub CommandButton1_Click()‘’窗体1控件代码

Unload Me

End Sub

Private Sub CommandButton2_Click()

Unload Me

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

x = Me.ActiveControl.Name

End Sub

------------------

Sub 关闭方法1()

Application.DisplayAlerts = False

ThisWorkbook.Save

Application.Quit

ThisWorkbook.Close

Application.DisplayAlerts = True

End Sub

'---------------------------------------------

Sub 关闭方法2()

Application.DisplayAlerts = False

ThisWorkbook.Save

'Application.Quit

Application.DisplayAlerts = True

Shell "taskkill /f /im EXCEL.EXE"

End Sub

是这样的,Close和Quit方法,是可以亏茄退出工作簿,和直接彻底关掉Excel的Application的背景界面的,但是必须有先后顺序,你得先用Quit再接Close语句(比如上面方法1),原因是:用了Close之后,再接Quit,你的VBA工程就没有对象支持了,剩下任何代码都无法继续依托Excel对象运行。也可以用另一种方法(比如上面方法2),你用代码保存文档,然后用Bat(批处理)里面的方法,也就是用taskkill /F /IM “程序名称”方法在保存文档之后强制关闭Excel应用界面敏空,这种方法比较彻底桥空瞎,无残留。

on error GOTO A

A:msgbox()

在调用的帆帆穗前面on error GOTO A

在SUB结束前面

A:msgbox()‘提示

end'结束轿烂程序

end sub

==============态卜===

你直接吧A放到你程序的结束地方不就好了


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12406280.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存