在vba运行时冻结excel编辑

我有一个Excel表,我想编辑通过VB脚本,但我需要用户不能编辑任何单元格或在脚本运行时单击任何button。 由于在excelbutton单击后我popup一个程序,在此VBA子运行期间启用了编辑。 我需要的是一种编程方式,用VBA来locking所有的单元格进行编辑。 我怎样才能做到这一点?

Psuedo_Code:

Shell _ "PROGRAM1" Shell _ "PROGRAM2" Dim shellobject As PROGRAM2 shellobject.Connect "PROGRAM1" shellobject.dothings if(shellobject.success) then Cells(1,1).Value = "HUZZAH!" Else Cells(1,1).Value = "OH NO MR BILL!" End If shellobject.dootherthings etc..... 

在使用用户窗体时,将其设置为窗体的“属性”对话框中的模态 (如果不可见,请按F4键):

在这里输入图像说明

这样,除非closures,否则用户不能在表单外单击Excel。

此外,您可以尝试在执行过程中禁用button。 这段代码应该这样做:

 Private Sub CommandButton1_Click() SetButtonsEnabled False Application.EnableEvents = False 'Your code here SetButtonsEnabled Application.EnableEvents = True End Sub Private Sub SetButtonsEnabled(Optional blnEnable As Boolean = True) Dim btn As Control For Each btn In Me.Controls If TypeOf btn Is CommandButton Then btn.Enabled = blnEnable Next End Sub 

你有没有考虑禁用从键盘input。 这可能是一种完成你所需要的蛮力的方式,但这是一个想法。

您可以使用

 Application.DataEntryMode = 

禁用键盘input。 在脚本启动之后将其设置为closures状态,然后在脚本结束之前将其closures。

再次,这可以被视为暴力,但只是一个想法。

希望有所帮助。

以下为我工作:

在开始处使用Application.Interactive = False ,并在最后重新启用它。 这将禁止用户点击/input尝试,直到macros/用户窗体/任何代码部分完成其操作。

我正在试着对Steventnorris和Even-Steven发表评论,但是我不能,所以只是在这里回答。

 Application.DataEntryMode= xlOff in the beginning should be set to Application.DataEntryMode= xlOn and then at the end of the code put in Application.DataEntryMode= xlOff 

我只是试过这个,它已经阻止我编辑工作表,以防其他人在尝试。