我有用于在Excel工作表中隐藏和取消隐藏行的代码。在user1302114(Unhide rows one by one)的帮助下,代码已缩短,现在可以更快地运行。
当前问题是:
NewUnhideJobs
(第二部分的位置)HideAllJobs
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
答案 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 = 0
对NewUnhideJobs
中的计数器没有任何影响,因为您已经声明了2个不同的counter
变量,每个过程/子变量一个。