受保护工作表

时间:2016-04-10 09:27:48

标签: excel vba excel-vba

我在Excel中有一个复选框,点击后隐藏/取消隐藏特定行。宏以下列方式工作:

  1. 在列(1)中查找包含文本" FirstRow"
  2. 的行号
  3. 隐藏/取消隐藏下面的第4,第8,第12等......行

    Sub rachna_redakcia_Click()
    'Define Dim
    Dim myRange As Range
    Dim FirstRow as Long
    Dim rachna_redakcia_box As Long
    Dim R1,R2....R25 as Range
    
    Sheets(ActiveSheet.Name).Protect contents:=True, userinterfaceonly:=True
    rachna_redakcia_box = ActiveSheet.Shapes("Rachna redakcia Box").ControlFormat.Value
    'Find Row number
    With Sheets(ActiveSheet.Name).Columns(1)
        Set FindRow = .Find("FirstRow")
    End With
    
    FirstRow = FindRow.Row + 1
    Set R1 = Rows(FirstRow + 4)
    Set R2 = Rows(FirstRow + 8)
    Set R3 = Rows(FirstRow + 12)
    Set R4 = Rows(FirstRow + 16)
    Set R5 = Rows(FirstRow + 20)
    Set R6 = Rows(FirstRow + 24)
    Set R7 = Rows(FirstRow + 28)
    Set R8 = Rows(FirstRow + 32)
    Set R9 = Rows(FirstRow + 36)
    Set R10 = Rows(FirstRow + 40)
    Set R11 = Rows(FirstRow + 44)
    Set R12 = Rows(FirstRow + 48)
    Set R13 = Rows(FirstRow + 52)
    Set R14 = Rows(FirstRow + 56)
    Set R15 = Rows(FirstRow + 60)
    Set R16 = Rows(FirstRow + 64)
    Set R17 = Rows(FirstRow + 68)
    Set R18 = Rows(FirstRow + 72)
    Set R19 = Rows(FirstRow + 76)
    Set R20 = Rows(FirstRow + 80)
    Set R21 = Rows(FirstRow + 84)
    Set R22 = Rows(FirstRow + 88)
    Set R23 = Rows(FirstRow + 92)
    Set R24 = Rows(FirstRow + 96)
    Set R25 = Rows(FirstRow + 100)
    
    Set myRange = Union(R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17, R18, R19, R20, R21, R22, R23, R24, R25)
    
    If rachna_redakcia_box = 1 Then
        myRange.EntireRow.Hidden = False
    End If
    If rachna_redakcia_box = -4146 Then
        myRange.EntireRow.Hidden = True
    End If
    End Sub
    
  4. 当工作表不受保护时,宏适合我的需要,但是当我保护它时,我收到错误:

      

    运行时错误' 91'对象变量或未设置块变量

    调试器说问题就在这一行:

      

    FirstRow = FindRow.Row + 1

    我添加了以下行,但这只解决了在Mac上运行的Excel而不是Windows中的问题:

      

    表格(ActiveSheet.Name).Protect contents:= True,userinterfaceonly:= True

    任何帮助都将不胜感激。

0 个答案:

没有答案
相关问题