【问题标题】:Deleting Hidden Rows in a Selected Range删除选定范围内的隐藏行
【发布时间】:2020-06-20 13:36:30
【问题描述】:

我想删除选定范围内的所有隐藏行。这里选择的范围是用户通过输入框选择的范围。在我的循环中,我从 LastRow 移动到 StartRow。 我在定义行时遇到问题,因为我希望 StartRow 成为该选定范围的第一行,而 LastRow 成为选定范围区域末尾的最后一行 代码给出了不匹配错误“13”。

我是 VBA 新手,可能在下面的代码中犯了一些愚蠢的错误。

Sub Delete_Hidden_Row()
Dim LastRow As Long
Dim StartRow As Long
Dim r As Long
Dim MyRange As range

Set MyRange = Application.InputBox(Prompt:="Select the first Cell (Hidden Rows in the region of the 
selected cell will be deleted) ", _
Title:="Delete Hidden Rows", Type:=8)

StartRow = range(MyRange.Rows, MyRange.Columns).Rows.EntireRow
LastRow = range(MyRange.Rows, MyRange.Columns).CurrentRegion.Rows.Count + 1
For r = LastRow To StartRow
    If Rows(r).Hidden = True Then
        Rows(r).Delete
        
    End If
Next r

【问题讨论】:

  • 你尝试分配 range(..).Rows.EntireRow (它返回一个 Range 对象给 LastRow 这是一个 Long 数字,所以你有一个不匹配

标签: excel vba


【解决方案1】:

删除行时,您需要稍微调整循环。如果您删除第一行,则第二行将成为第一行,并且您的上限变得太大,因此您会收到错误。

最简单的修复方法是向后循环,从最后一行开始向后工作。

另一种方法是在遇到可见行并移动到下一行之前不增加行计数器。

更新

以下是我提到的方法的示例。在使用范围时,row 是相对于范围而言的,因此它总是从 1 到行数。

Sub Delete_Hidden_One()
Dim TargetRange As Range
Set TargetRange = Application.InputBox(Prompt:="Select the first Cell (Hidden Rows in the region of the selected cell will be deleted) ", Title:="Delete Hidden Rows", Type:=8)
Dim Row As Integer
Dim TargetStart As Integer
TargetStart = TargetRange.Rows.Count
For Row = TargetStart To 1 Step -1
    If TargetRange.Rows(Row).EntireRow.Hidden = True Then
        Debug.Print "Deleting Row <" & Row & ">"
        TargetRange.Rows(Row).EntireRow.Delete xlShiftUp
    Else
        Debug.Print "Skipping Row <" & Row & ">"
    End If
Next Row
End Sub

Sub Delete_Hidden_Two()
Dim TargetRange As Range
Set TargetRange = Application.InputBox(Prompt:="Select the first Cell (Hidden Rows in the region of the selected cell will be deleted) ", Title:="Delete Hidden Rows", Type:=8)
Dim Row As Integer
Row = 1
Do
    If TargetRange.Rows(Row).EntireRow.Hidden = True Then
        Debug.Print "Row <" & Row & "> Is Hidden - Deleting"
        TargetRange.Rows(Row).EntireRow.Delete xlShiftUp
    Else
        If Row < TargetRange.Rows.Count Then
            Debug.Print "Row <" & Row & "> Is Not Hidden - Incrementing"
            Row = Row + 1
        Else
            Debug.Print "Row <" & Row & "> Is Out-Of-Bounds - Exiting"
            Exit Do
        End If
    End If
Loop
End Sub

【讨论】:

  • 感谢您的回复我更新了我的代码以从 lastrow 移动到 startrow 你能建议我如何定义我的 startrow 和 lastrow 问题已相应更新以更清晰
【解决方案2】:

这应该能让你到达那里:

StartRow = MyRange.Cells(1,1).Row
LastRow = MyRange.Cells(myRange.Rows.Count,1).Row

For r = LastRow To StartRow
    If r.EntireRow.Hidden Then 
        Dim remove as Range
        If remove Is Nothing Then
            Set remove = r
        Else
            Set remove = Union(remove,r)
        End If        
    End If
Next r

remove.EntireRow.Delete

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 2015-11-07
    • 2013-09-05
    • 1970-01-01
    • 2019-09-08
    • 2020-05-01
    相关资源
    最近更新 更多