vba Excel - 运行时错误“1004”:无法设置范围类的隐藏属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13967033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:38:19  来源:igfitidea点击:

Excel - Run-time error '1004': Unable to set the hidden property of the range class

excelvbaexcel-vba

提问by user1917946

I am new to scripting and I am trying to improve a existing Macro. I recorded a macro to remove duplicate and added it in a Main function which calls some other functions, but I am getting this error when I add the macro I recorded:

我是脚本的新手,我正在尝试改进现有的宏。我录制了一个宏以删除重复项并将其添加到调用其他一些函数的 Main 函数中,但是当我添加我录制的宏时出现此错误:

Run-time error '1004': Unable to set the hidden property of the range class

The code looks like

代码看起来像

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim changed As Range
    Set changed = Intersect(Target, Range("J15"))
    If Not changed Is Nothing Then
        Range("A48:A136").EntireRow.Hidden = True
        Select Case Target.Value
            Case "Agriculture"
                Range("A48:A96").EntireRow.Hidden = False
            Case "Commercial"
                Range("A97:A136").EntireRow.Hidden = False
            Case "MDP"
                Range("A48:A61").EntireRow.Hidden = False
        End Select
        Range("J15").Select
    End If
End Sub

回答by Andy Brown

Some possible answers:

一些可能的答案:

  • You have a comment in one of the selected cells
  • You have some drawn objects which don't resize with text
  • Your worksheet is protected
  • 您在所选单元格之一中有评论
  • 您有一些不随文本调整大小的绘制对象
  • 您的工作表受到保护

When you set a breakpoint on the first line of the event handler, and then press F8 to step through the macro, I'm assuming it crashes on the line:

当您在事件处理程序的第一行设置断点,然后按 F8 以单步执行宏时,我假设它在该行崩溃:

Range("A48:A136").EntireRow.Hidden = True

回答by Krystian Tambur

try this :)

尝试这个 :)

Private Sub Worksheet_Change(ByVal Target As Range)
ActiveWorkbook.Unprotect "password_here"
    Dim changed As Range
    Set changed = Intersect(Target, Range("J15"))
    If Not changed Is Nothing Then
        Range("A48:A136").EntireRow.Hidden = True
        Select Case Target.Value
            Case "Agriculture"
                Range("A48:A96").EntireRow.Hidden = False
            Case "Commercial"
                Range("A97:A136").EntireRow.Hidden = False
            Case "MDP"
                Range("A48:A61").EntireRow.Hidden = False
        End Select
        Range("J15").Select
    End If
ActiveWorkbook.Protect "password_here"
End Sub

This should work for you :)

这应该对你有用:)