我创建了一个报告模板,允许用户选择各种过滤器,然后根据这些过滤器创建特定报告。以下是过滤器选择按钮的代码:
Sub Filter_Select()
rptSelect.Show
End Sub
在此之后,会向用户显示用户选项以选择不同的过滤器。每个UserForm调用下一个用户表单,直到它们到达最后一个代码如下的UserForm:
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
一旦他们选择了最终过滤器,就会运行Create_Report宏来确定要创建的报告以及根据所选过滤器使用的公式。代码是这样的:
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
所有代码都正常运行并且报告已创建,但我遇到了一个错误,当我尝试滚动代码运行后显示的工作表时,滚动条会移动,但单元格似乎卡在原位。如果我尝试选择一个单元格并使用箭头键移动,则选择会跳转。这两件事都可以通过转到另一张纸然后再回来来解决。
此外,在此代码运行后,我无法通过任务管理器以外的任何方法关闭Excel。因为当我第一次打开工作簿时没有发生这种情况,我只能假设上面的代码以某种方式导致了这一点。
答案 0 :(得分:1)
很抱歉,花了这么长时间才回到这里。过去一周我被淹没了。无论如何,由于来自@Comintern的指示,我终于解决了这个问题。基本上,我从我的每个表单中取出了所有繁琐的工作,并将它放在过滤器按钮调用的模块中。
这不仅是一种更加简化和高效的代码处理方式,它还提供了一个集中的位置来评估可能出现的任何错误,从根本上消除了一些可能的故障点。下面是过滤器按钮的代码和其中一个过滤器表单的代码。
新表单代码:
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
过滤按钮代码:
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
我确信除了它在这里出现的方式之外,可能有一种更清晰的方式来写这个,欢迎任何人可能有任何建议让它看起来更好,但至少现在,它的工作就像一个魅力。感谢大家的投入和耐心。