运行宏后,无法在工作表上使用鼠标滚轮滚动

时间:2015-07-29 08:49:23

标签: excel vba excel-vba

我使用VBA创建了一个面试任务,根据所选标准自动生成面试问题列表。

问题的选择和列表都是通过用户表单和列表框完成的,它背后的VBA最终生成一个基于隐藏模板的工作表,我将其存储为非常隐藏。

在脚本结束时,当它从新工作簿中生成模板时,它工作正常,但我注意到我无法正确滚动工作簿,因为我的鼠标滚轮被禁用。手动解决方案是按下VBA代码上的STOP按钮,但我不明白是什么原因导致这个打嗝首先发生。

生成表单的代码如下所示,有没有人在工作簿停止响应之前遇到此问题,或者鼠标滚轮停止,除非单击VBA窗口中的停止按钮?

代码

Sub PopulateForm()
Dim WB As Workbook
Dim WS As Worksheet, ws2 As Worksheet, ws3 As Worksheet, WS4 As Worksheet
Dim newbook As Workbook

DoEvents

Set WB = ThisWorkbook
Set WS = WB.Worksheets("Interview Questions")
Set ws3 = WB.Worksheets("Config")
Set WS4 = WB.Worksheets("Questions")


If WS.Visible = xlSheetHidden Or WS.Visible = xlSheetVeryHidden Then
WS.Visible = xlSheetVisible
End If

If ws3.Visible = xlSheetVisible Then
ws3.Visible = xlSheetHidden
End If

If WS4.Visible = xlSheetVisible Then
WS4.Visible = xlSheetHidden
End If

Set newbook = Workbooks.Add
WS.Copy Before:=newbook.Sheets(1)

newbook.Sheets("Sheet1").Delete

newbook.Sheets(1).Cells(103, 3).Copy
newbook.Sheets(1).Cells(103, 3).PasteSpecial (xlPasteValues)

newbook.Sheets(1).Cells(110, 3).Copy
newbook.Sheets(1).Cells(110, 3).PasteSpecial (xlPasteValues)

newbook.Sheets(1).Cells(117, 3).Copy
newbook.Sheets(1).Cells(117, 3).PasteSpecial (xlPasteValues)

newbook.Sheets(1).Cells(124, 3).Copy
newbook.Sheets(1).Cells(124, 3).PasteSpecial (xlPasteValues)

newbook.Sheets(1).Cells(131, 3).Copy
newbook.Sheets(1).Cells(131, 3).PasteSpecial (xlPasteValues)

newbook.Sheets(1).Cells(138, 3).Copy
newbook.Sheets(1).Cells(138, 3).PasteSpecial (xlPasteValues)


Call CleanUp.FormClose


newbook.Activate - **Mouse wheel stops here**
End Sub



Sub FormClose()
'Declare Variables
Dim WB1 As Workbook
Dim Config As Worksheet, Questions As Worksheet, Generator As Worksheet
Dim objLoop As Object

'Set Objects to Variables
Set WB1 = ThisWorkbook
Set Config = WB1.Worksheets("Config")
Set Questions = WB1.Worksheets("Questions")
Set Generator = WB1.Worksheets("Interview Question Generator")
Set InterviewQuestions = WB1.Worksheets("Interview Questions")

'Make configuration sheets very hidden
If Config.Visible = xlSheetVisible Then
   Config.Visible = xlSheetVeryHidden
End If

If Questions.Visible = xlSheetVisible Then
   Questions.Visible = xlSheetVeryHidden
End If

If InterviewQuestions.Visible = xlSheetVisible Then
   InterviewQuestions.Visible = xlSheetVeryHidden
End If

For Each objLoop In VBA.UserForms
    If TypeOf objLoop Is UserForm Then
       Unload objLoop
    End If
Next objLoop

Application.ScreenUpdating = True

DoEvents

Generator.Activate

End Sub

1 个答案:

答案 0 :(得分:1)

如果要在用户窗体处于活动状态时滚动,则必须无模式显示。从Excel帮助文件:

"当UserForm是模态的时,用户必须在使用应用程序的任何其他部分之前做出响应。"