最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

excel - in one column, several groups of hidden rows, insert new row and they all move down, how to stop? - Stack Overflow

programmeradmin0浏览0评论

I have several categories which I want the BTN_X to hide or unhide rows associated with each category. The code below works, but if i want to add an additional row (say add 217) in the first category, the location shifts all rows below it. How can i make the ROWS(xx:xx).select variable as to not effect the rest of the worksheet? I would like to add a second button used to add new row. Thinking this would evaluate and be an n+1 to all the locations below, but seems overly complicated. thoughts?

'Category 1
Sub BTN_1()
    Rows("200:216").Select

    If Selection.EntireRow.Hidden = True Then
        Selection.EntireRow.Hidden = False
    Else
        Selection.EntireRow.Hidden = True
    End If
End Sub

'Category 2
Sub BTN_2()

    Rows("218:234").Select

    If Selection.EntireRow.Hidden = True Then
        Selection.EntireRow.Hidden = False
    Else
        Selection.EntireRow.Hidden = True
    End If
End Sub
``

insert row and the lower categories' lost row range

I have several categories which I want the BTN_X to hide or unhide rows associated with each category. The code below works, but if i want to add an additional row (say add 217) in the first category, the location shifts all rows below it. How can i make the ROWS(xx:xx).select variable as to not effect the rest of the worksheet? I would like to add a second button used to add new row. Thinking this would evaluate and be an n+1 to all the locations below, but seems overly complicated. thoughts?

'Category 1
Sub BTN_1()
    Rows("200:216").Select

    If Selection.EntireRow.Hidden = True Then
        Selection.EntireRow.Hidden = False
    Else
        Selection.EntireRow.Hidden = True
    End If
End Sub

'Category 2
Sub BTN_2()

    Rows("218:234").Select

    If Selection.EntireRow.Hidden = True Then
        Selection.EntireRow.Hidden = False
    Else
        Selection.EntireRow.Hidden = True
    End If
End Sub
``

insert row and the lower categories' lost row range
Share Improve this question edited Mar 27 at 21:49 Tim Williams 167k8 gold badges100 silver badges141 bronze badges asked Mar 27 at 21:46 user30085781user30085781 111 bronze badge 1
  • 1 You could use named ranges "block1" , "block2" etc instead of row numbers. – Tim Williams Commented Mar 27 at 21:50
Add a comment  | 

1 Answer 1

Reset to default 1

If you use named ranges instead of row numbers you can do something like this:

Sub BTN_1()
    ToggleRowVis "Block_1"
End Sub

Sub BTN_2()
    ToggleRowVis "Block_2"
End Sub

'Hide show rows for the named range `nm`
Sub ToggleRowVis(nm As String)
    With Range(nm).EntireRow
        .Hidden = Not .Hidden
    End With
End Sub

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论