簡體   English   中英

Excel無法隱藏超過53行

[英]Excel fails to hide more than 53 rows

這是代碼:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$2" Then
        If Range("A2").Value = "Select" Then
            ActiveSheet.Rows("5:61").Select
            Selection.EntireRow.Hidden = True
        End If
    End If
End Sub

由於某種原因,如果范圍大於“ 5:58”,則會收到運行時錯誤“ 1004”。

這段代碼在Excel 2013中運行,很遺憾,我沒有其他版本。 由於我不喜歡引用活動工作簿和活動工作表以及使用選擇,因此您也可以使用

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$2" Then
        If Range("A2").Value = "Select" Then
            Target.Parent.Rows("5:61").EntireRow.Hidden = True
        End If
    End If
End Sub

Target.Parent是指包含范圍的工作表。

您正在使用哪個版本的Excel? 我在Excel 2007上嘗試了您的代碼,它可以正常工作。 我刪除了選擇線,並以此方式進行了嘗試,這也可行。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$2" Then
        If Range("A2").Value = "Select" Then
            ActiveSheet.Rows("5:61").EntireRow.Hidden = True
        End If
    End If
End Sub

代碼正確無誤。

該問題是由於我必須隱藏許多行/列而引起的。 Excel僅允許您隱藏有限的數量。

為您提供簡單的解決方法:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
    If Range("A2").Value = "Select" Then
        Dim Rg as Range
        For each Rg in Target.Parent.Rows("5:61") 'not sure if you need to add a .rows here, just try
            Rg.EntireRow.Hidden = True
        next Rg
    End If
End If
End Sub

這樣,它一次只隱藏一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM