按逻辑顺序取消隐藏行

时间:2019-02-26 08:32:05

标签: excel vba

我有用于在Excel工作表中隐藏和取消隐藏行的代码。在user1302114(Unhide rows one by one)的帮助下,代码已缩短,现在可以更快地运行。

当前问题是:

  1. 运行两次NewUnhideJobs(第二部分的位置)
  2. 运行一次HideAllJobs
  3. 运行NewUnhideJobs

因此,我们在第三部分的位置而不是应该的第一位置。因此NewUnhideJobs并非从0开始,而是继续执行先前的过程。

曾尝试删除Static counter As Byte,但没有它,代码将无法工作。

counter = 0也曾在HideAllJobs中尝试,但没有成功。

隐藏行:

Sub NewUnhideJobs()
Static counter As Byte
    Dim RngTxt As String, RngAR() As String, ThisRng As String

    counter = (counter + 1) Mod 26

    ThisRng = "" & (174 - (counter * 5)) & ":" & (174 - (counter * 5) + 4)

    Application.ScreenUpdating = False
    ThisWorkbook.Sheets("Filling form").Unprotect
    ThisWorkbook.Sheets("Filling form").Rows(ThisRng).EntireRow.Hidden = False
    ThisWorkbook.Sheets("Filling form").Protect
    Application.ScreenUpdating = True
End Sub

取消隐藏行:

Sub HideAllJobs()
   Static counter As Byte
   Application.ScreenUpdating = False
   ThisWorkbook.Sheets("Filling form").Unprotect
   Rows("49:173").EntireRow.Hidden = True
   counter = 0
   ThisWorkbook.Sheets("Filling form").Protect
   Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:2)

您必须将计数器声明为全局变量。

Public counter As Long 'public variable

Sub NewUnhideJobs()
    Dim RngTxt As String, RngAR() As String, ThisRng As String

    counter = (counter + 1) Mod 26

    ThisRng = "" & (174 - (counter * 5)) & ":" & (174 - (counter * 5) + 4)

    Application.ScreenUpdating = False
    ThisWorkbook.Sheets("Filling form").Unprotect
    ThisWorkbook.Sheets("Filling form").Rows(ThisRng).EntireRow.Hidden = False
    ThisWorkbook.Sheets("Filling form").Protect
    Application.ScreenUpdating = True
End Sub


Sub HideAllJobs()
   Application.ScreenUpdating = False
   ThisWorkbook.Sheets("Filling form").Unprotect
   Rows("49:173").EntireRow.Hidden = True
   counter = 0
   ThisWorkbook.Sheets("Filling form").Protect
   Application.ScreenUpdating = True
End Sub

否则,您的counter = 0NewUnhideJobs中的计数器没有任何影响,因为您已经声明了2个不同的counter变量,每个过程/子变量一个。