优化冗余Excel VBA代码

时间:2015-07-24 20:09:42

标签: excel vba excel-vba

用于学习目的的快速问题。我下面的代码工作正常,但我很确定这是我的工作簿开始变慢的几个原因之一。

基本上,在定义的范围单元格“propcount”上设置一个公式,该单元格返回一个数字。我想根据这个数字显示特定的表格,但下面的代码非常多余。有人可以帮我解决吗?另请注意,表#不一定是有序的。目前,它影响到14到29页,但它跳过28;在未来,它可能变得越来越没有组织。

有什么想法吗?

Private Sub Worksheet_Calculate()

If Range("propcount") = "0" Then
    Sheet14.Visible = xlVeryHidden
    Sheet15.Visible = xlVeryHidden
    Sheet16.Visible = xlVeryHidden
    Sheet17.Visible = xlVeryHidden
    Sheet18.Visible = xlVeryHidden
    Sheet19.Visible = xlVeryHidden
    Sheet20.Visible = xlVeryHidden
    Sheet21.Visible = xlVeryHidden
    Sheet22.Visible = xlVeryHidden
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "1" Then
    Sheet14.Visible = True
    Sheet15.Visible = xlVeryHidden
    Sheet16.Visible = xlVeryHidden
    Sheet17.Visible = xlVeryHidden
    Sheet18.Visible = xlVeryHidden
    Sheet19.Visible = xlVeryHidden
    Sheet20.Visible = xlVeryHidden
    Sheet21.Visible = xlVeryHidden
    Sheet22.Visible = xlVeryHidden
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "2" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = xlVeryHidden
    Sheet17.Visible = xlVeryHidden
    Sheet18.Visible = xlVeryHidden
    Sheet19.Visible = xlVeryHidden
    Sheet20.Visible = xlVeryHidden
    Sheet21.Visible = xlVeryHidden
    Sheet22.Visible = xlVeryHidden
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "3" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = xlVeryHidden
    Sheet18.Visible = xlVeryHidden
    Sheet19.Visible = xlVeryHidden
    Sheet20.Visible = xlVeryHidden
    Sheet21.Visible = xlVeryHidden
    Sheet22.Visible = xlVeryHidden
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "4" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = xlVeryHidden
    Sheet19.Visible = xlVeryHidden
    Sheet20.Visible = xlVeryHidden
    Sheet21.Visible = xlVeryHidden
    Sheet22.Visible = xlVeryHidden
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "5" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = xlVeryHidden
    Sheet20.Visible = xlVeryHidden
    Sheet21.Visible = xlVeryHidden
    Sheet22.Visible = xlVeryHidden
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "6" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = xlVeryHidden
    Sheet21.Visible = xlVeryHidden
    Sheet22.Visible = xlVeryHidden
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "7" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = True
    Sheet21.Visible = xlVeryHidden
    Sheet22.Visible = xlVeryHidden
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "8" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = True
    Sheet21.Visible = True
    Sheet22.Visible = xlVeryHidden
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "9" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = True
    Sheet21.Visible = True
    Sheet22.Visible = True
    Sheet23.Visible = xlVeryHidden
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "10" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = True
    Sheet21.Visible = True
    Sheet22.Visible = True
    Sheet23.Visible = True
    Sheet24.Visible = xlVeryHidden
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "11" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = True
    Sheet21.Visible = True
    Sheet22.Visible = True
    Sheet23.Visible = True
    Sheet24.Visible = True
    Sheet25.Visible = xlVeryHidden
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "12" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = True
    Sheet21.Visible = True
    Sheet22.Visible = True
    Sheet23.Visible = True
    Sheet24.Visible = True
    Sheet25.Visible = True
    Sheet26.Visible = xlVeryHidden
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "13" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = True
    Sheet21.Visible = True
    Sheet22.Visible = True
    Sheet23.Visible = True
    Sheet24.Visible = True
    Sheet25.Visible = True
    Sheet26.Visible = True
    Sheet27.Visible = xlVeryHidden
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "14" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = True
    Sheet21.Visible = True
    Sheet22.Visible = True
    Sheet23.Visible = True
    Sheet24.Visible = True
    Sheet25.Visible = True
    Sheet26.Visible = True
    Sheet27.Visible = True
    Sheet29.Visible = xlVeryHidden
ElseIf Range("propcount") = "15" Then
    Sheet14.Visible = True
    Sheet15.Visible = True
    Sheet16.Visible = True
    Sheet17.Visible = True
    Sheet18.Visible = True
    Sheet19.Visible = True
    Sheet20.Visible = True
    Sheet21.Visible = True
    Sheet22.Visible = True
    Sheet23.Visible = True
    Sheet24.Visible = True
    Sheet25.Visible = True
    Sheet26.Visible = True
    Sheet27.Visible = True
    Sheet29.Visible = True
End If

End Sub

4 个答案:

答案 0 :(得分:2)

E.g。

Sub SetVis(propCount As Long)
    Dim arr, i As Long

    arr = Array(Sheet14, Sheet15, Sheet16, Sheet17, Sheet18, Sheet19, _
                Sheet20, Sheet21, Sheet22, Sheet23, Sheet24, Sheet25, _
                Sheet26, Sheet27, Sheet29)

    'assuming lbound=0 (ie, not using Option Base 1)
    For i = LBound(arr) To UBound(arr)
        arr(i).Visible = IIf(i < propCount, True, xlVeryHidden)
    Next i

End Sub

请致电:

Private Sub Worksheet_Calculate()
    SetVis clng(Me.Range("propcount").Value
End Sub

应该对单元格值等添加一些检查。

答案 1 :(得分:1)

循环设置它们的纸张。从第一张纸圈到支架圈,使它们可见。这样的事情。

Dim ws As Excel.Worksheet
Dim iIndex as Integer
if Range("propcount") > 0 then
    For iIndex = 14 To 14 + Range("propcount")

        Set ws = Worksheets(iIndex)
        ws.Visible = true

    Next iIndex
End if

或许你想要隐藏它们。在这种情况下,你可以从propcount转到最后一张。

Dim ws As Excel.Worksheet
Dim iIndex as Integer
if Range("propcount") > 0 then
    For iIndex = 14 + Range("propcount") To ActiveWorkbook.Worksheets.count

        Set ws = Worksheets(iIndex)
        ws.Visible = xlVeryHidden

    Next iIndex
End if

如果表格始终处于可靠状态,您可能希望同时使用显示和隐藏。

Dim ws As Excel.Worksheet
Dim iIndex as Integer
if Range("propcount") > 0 then
    For iIndex = 14 To 14 + Range("propcount")
        Set ws = Worksheets(iIndex)
        ws.Visible = true
    Next iIndex

    For iIndex = 14 + Range("propcount") To ActiveWorkbook.Worksheets.count
        Set ws = Worksheets(iIndex)
        ws.Visible = xlVeryHidden
    Next iIndex
End if

答案 2 :(得分:1)

您可能希望在数组中定义工作表名称。您甚至可以将配置存储在文本或ini文件中,并在功能开始时阅读。

例如:

Dim a(15) As String
...
a(3) = "/14/15/16/"       ' Just store the sheet numbers that need to be visible
a(4) = "/14/15/16/17/"    ' Delimit them with any character you like
...

这为您提供了很大的灵活性。您不必依赖于您的工作表名称是连续的,因为正如您在Sheet28中找到的那样,保持秩序并不总是那么容易。

然后,只需根据您的propcount值获取正确的字符串:

Dim strSheetList As String
strSheetList = a(Range("propcount"))

最后,迭代所有工作表并检查每个工作表以查看是否需要显示:

Dim sh As Worksheet, strSheetNum As String

For Each sh In Worksheets
    strSheetNum = Mid$(sh.CodeName, 6) 
    If InStr(strSheetList, "/" & strSheetNum & "/") Then
        sh.Visible = True
    Else
        sh.Visible = xlVeryHidden
    End If
Next

答案 3 :(得分:1)

你也可以这样做:

Sheets14.Visible = IIf(propcount > 0, True, xlVeryHidden)
...

顺便说一句,减少此任务的代码行不会优化性能恕我直言。问题可能是您在事件On Calculate

上运行此代码