运行macros后Excel不会closures,工作表不会滚动,箭头键会导致select跳转

我创build了一个报告模板,允许用户select各种filter,然后根据这些filter创build特定的报告。 以下是filterselectbutton的代码:

Sub Filter_Select() rptSelect.Show End Sub 

之后,用户将看到用户窗体来select不同的filter。 每个用户窗体调用下一个用户窗体,直到他们到达代码是这样的最后一个用户窗体:

 Private Sub UserForm_Activate() With milSelect .Top = Application.Top + 250 .Left = Application.Left + 250 End With End Sub -------------------------------------------------- Private Sub UserForm_Initialize() milDV.RowSource = Range("Q1:Q8").Address End Sub -------------------------------------------------- Private Sub CancelmilSelect_Click() Unload Me End Sub -------------------------------------------------- Private Sub OKmilSelect_Click() Sheets("Report Selection").Unprotect Range("B6").ClearContents Range("B6") = milDV.Text Unload Me Call Create_Report End Sub 

一旦他们select了最终的filter,运行Create_Reportmacros以确定要创build哪些报告以及基于所选filter使用哪些公式。 代码是这样的:

 Option Explicit Sub Create_Report() ' ' Macro to update fields in different reporting sections after user selects report type ' ' Application.EnableEvents = False Application.Calculation = xlCalculationManual Application.ScreenUpdating = False ' ' Display Tabs ' Dim z As Integer For z = 2 To Worksheets.Count Sheets(z).Visible = xlSheetHidden Next z If Range("B1").Value = "National" And Range("B6") = "Military" Then Sheets("Program Summary").Visible = xlSheetVisible Else If Range("B1") = "National" Then Sheets("Program Summary").Visible = xlSheetVisible Sheets("Family & Household Demographics").Visible = xlSheetVisible Sheets("Registered Member Dues").Visible = xlSheetVisible Else Sheets("Program Summary").Visible = xlSheetVisible Sheets("Summary").Visible = xlSheetVisible Sheets("Financial Summary").Visible = xlSheetVisible End If End If ' ' Financial Summary ' If Sheets("Financial Summary").Visible = True Then Sheets("Financial Summary").Select Sheets("Financial Summary").Unprotect ' ' Name Ranges ' Dim Operating_Expenses, Income_Private1, Income_Private2, Income_Private3, Income_Gov, Income_Other1, Income_Other2 As Range Set Operating_Expenses = Range("C5:C7") Set Income_Private1 = Range("C13") Set Income_Private2 = Range("C14:C16") Set Income_Private3 = Range("C17") Set Income_Gov = Range("C19:C22") Set Income_Other1 = Range("C25:C26") Set Income_Other2 = Range("C27:C30") ' ' State Non-Military Financial Summary ' If Worksheets("Report Selection").Range("B1").Value = "State" Then If Worksheets("Report Selection").Range("B6").Value = "Non-Military" Then Range("B1").Value = Worksheets("Report Selection").Range("B4").Value Range("C1").Value = "Non-Military" Operating_Expenses.Formula = "=SUMIFS(INDEX('Financial Data'!$BE:$BG, 0, ROW(1:1)),'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Income_Private1.Formula = "=SUMIFS(INDEX('Financial Data'!$AO:$AO, 0, ROW(1:1)),'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Income_Private2.Formula = "=SUMIFS(INDEX('Financial Data'!$AL:$AN, 0, ROW(1:1)),'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Income_Private3.Formula = "=SUMIFS(INDEX('Financial Data'!$AQ:$AQ, 0, ROW(1:1)),'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Income_Gov.Formula = "=SUMIFS(INDEX('Financial Data'!$BA:$BD, 0, ROW(1:1)),'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Range("C24").Formula = "=SUMIFS('Financial Data'!$AP:$AP,'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Income_Other1.Formula = "=SUMIFS(INDEX('Financial Data'!$AV:$AW, 0, ROW(1:1)),'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Income_Other2.Formula = "=SUMIFS(INDEX('Financial Data'!$AR:$AU, 0, ROW(1:1)),'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Range("C31").Formula = "=SUMIFS('Financial Data'!$AZ:$AZ,'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Range("C32").Formula = "=SUMIFS('Financial Data'!$AY:$AY,'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" Range("C33").Formula = "=SUMIFS('Financial Data'!$AK:$AK,'Financial Data'!$E:$E,$B$1,'Financial Data'!$H:$H,""0"")" End If End If End If ' ' Select First Sheet of Report ' Dim i As Integer Dim ws As Worksheet For i = 1 To Worksheets.Count If Sheets(i).Visible = True Then Sheets(i).Protect End If Next i For i = 2 To Worksheets.Count If Sheets(i).Visible = True Then Sheets(i).Select Exit For End If Next i Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub 

所有的代码运行正常,报告创build,但我有一个错误,当我试图滚动代码运行后显示的工作表滚动条移动,但细胞似乎卡住的地方。 如果我尝试select一个单元格并使用箭头键移动,则select跳转。 这两件事都是通过去另一张纸然后回来解决的。

此外,此代码运行后,我不能通过任务pipe理器以外的任何方法closuresExcel。 由于当我第一次打开工作簿时,没有发生这种情况,我只能假设我的上面的代码是以某种方式造成的。

对不起,花了这么长时间才回到这个。 过去一周我一直在淹没。 无论如何,我终于解决了这个问题,感谢@Comintern的指导。 基本上,我把每一个表格中的所有咕噜声都放在了filterbutton所调用的模块中。

这不仅是一种更简洁高效的代码工作方式,还提供了一个集中的位置来评估任何可能出现的错误,从根本上消除几个可能的故障点。 下面是filterbutton的代码和其中一个filterforms的代码。

新的表单代码:

 Private Sub UserForm_Activate() With milSelect .Top = Application.Top + 250 .Left = Application.Left + 250 End With End Sub --------------------------------------------------- Private Sub UserForm_Initialize() milDV.RowSource = Range("Q1:Q8").Address End Sub Private Sub CancelmilSelect_Click() Unload Me End Sub --------------------------------------------------- Private Sub OKmilSelect_Click() Sheets("Report Selection").Unprotect Range("B6").ClearContents Range("B6") = milDV.Text Me.Hide End Sub 

过滤button代码:

 Sub Filter_Select() Dim ReportType As String Dim Region As String Dim ServiceUnit As String Dim State As String Dim Military As String With New rptSelect .Show vbModal If Not Cancel = True Then If Range("B1").Value = "National" Then With New milSelect .Show vbModal If Not Cancel = True Then Unload rptSelect Unload milSelect Call Create_Report End If End With Else If Range("B1").Value = "Service Unit" Then With New SUSelect .Show vbModal If Not Cancel = True Then With New milSelect .Show vbModal If Not Cancel = True Then Unload rptSelect Unload milSelect Unload SUSelect Call Create_Report End If End With End If End With Else If Range("B1").Value = "Regional" Then With New rgnSelect .Show vbModal If Not Cancel = True Then With New milSelect .Show vbModal If Not Cancel = True Then Unload rptSelect Unload milSelect Unload rgnSelect Call Create_Report End If End With End If End With Else If Range("B1").Value = "State" Then With New stateSelect .Show vbModal If Not Cancel = True Then With New milSelect .Show vbModal If Not Cancel = True Then Unload rptSelect Unload milSelect Unload stateSelect Call Create_Report End If End With End If End With End If End If End If End If End If End With End Sub 

我敢肯定,除了这里的performance之外,还有一种更清晰的方式可以写出来,并且欢迎任何人为了使它看起来更好而提出任何build议,但至less现在它就像一种魅力。 感谢大家的投入和耐心。