简体   繁体   English

隐藏行的动态范围

[英]Dynamic range for hiding rows

I have workbook where I have buttons for hiding and unhiding rows. 我有工作簿,我有隐藏和取消隐藏行的按钮。 So when rows are hided one button is also hided and then vise versa. 因此,当隐藏行时,也会隐藏一个按钮,然后反之亦然。 Then I have range that should be hided and unhided while clicking the buttons. 然后我点击按钮时应该隐藏和取消隐藏的范围。 Is there any way to make this somehow dynamic? 有什么方法可以让它以某种方式动态吗?

The problem is that I have like 40 buttons and once I am making some edit to calculator and adding a new row to my worksheet I have to rewrite all the ranges. 问题是我有40个按钮,一旦我对计算器进行一些编辑并在我的工作表中添加一个新行,我必须重写所有范围。 Ranges are in sequence so they goes like: 范围按顺序排列,如下所示:

Range("1254:1275").EntireRow.Hidden = True
Range("1254:1275").EntireRow.Hidden = False
next one
Range("1276:1298").EntireRow.Hidden = True
Range("1276:1298").EntireRow.Hidden = False
next one
Range("1299:1350").EntireRow.Hidden = True
Range("1299:1350").EntireRow.Hidden = False
etc.

Is it somehow possible to have something like +22 instead of 1254:1275 ? 是否可能有+22而不是1254:1275 Then +23 instead of 1276:1298 etc.? 那么+23而不是1276:1298等?

For 40 buttons I have 80 subs (1 for hide and 1 for unhide). 对于40个按钮,我有80个潜艇(1个用于隐藏,1个用于取消隐藏)。 Each operation in own sub like so: 在自己的子操作中的每个操作如下:

HIDE BUTTON: 隐藏按钮:

Sub WorkshopWork_HideMe()

Application.ScreenUpdating = False
ThisWorkbook.Sheets("Price calculation").Unprotect Password:="123"
Range("1254:1275").EntireRow.Hidden = True
ActiveSheet.Shapes("Rectangle: Rounded Corners 111").Visible = True
ActiveSheet.Shapes("Rectangle: Rounded Corners 233").Visible = False
ThisWorkbook.Sheets("Price calculation").Protect Password:="123"
Application.ScreenUpdating = True

End Sub

UNHIDE BUTTON: UNHIDE BUTTON:

Sub WorkshopwnWork_UnhideMe()

Application.ScreenUpdating = False
ThisWorkbook.Sheets("Price calculation").Unprotect Password:="123"
Range("1254:1275").EntireRow.Hidden = False
ActiveSheet.Shapes("Rectangle: Rounded Corners 111").Visible = False
ActiveSheet.Shapes("Rectangle: Rounded Corners 233").Visible = True

ActiveWindow.ScrollRow = 1254
ThisWorkbook.Sheets("Price calculation").Protect Password:="123"
Application.ScreenUpdating = True

End Sub

Yes, you could do somenthing like this to make rows index dynamic : 是的,您可以像这样进行somenthing以使rows index 动态

Dim startRow As Integer

startRow = 1200
Range(startRow + 54 & ":" & startRow + 75).EntireRow.Hidden = True
Range(startRow + 54 & ":" & startRow + 75).EntireRow.Hidden = False

You can declare your variable as a Public Constant and them use it in all your functions : 您可以将变量声明为Public Constant ,并在所有functions使用它:

'This goes at the start of a Module, outside any Sub or Function
Public Const startRow As Integer = 1200

'Example Button1
Sub mySubButton1()
    Range(startRow + 54 & ":" & startRow + 75).EntireRow.Hidden = True
    Range(startRow + 54 & ":" & startRow + 75).EntireRow.Hidden = False
End Sub

'Example Button2
Sub mySubButton2()
    Range(startRow + 100 & ":" & startRow + 125).EntireRow.Hidden = True
    Range(startRow + 100 & ":" & startRow + 125).EntireRow.Hidden = False
End Sub

Hope this helps. 希望这可以帮助。

Try using Application.Caller . 尝试使用Application.Caller

Sub btnS()

Set wsAuth = ThisWorkbook.Worksheets("Data")
    ColumnNr = wsAuth.Buttons(Application.Caller).TopLeftCell.Column
    RowNr = wsAuth.Buttons(Application.Caller).TopLeftCell.Row
    Range(RowNr + 54 & ":" & RowNr + 75).EntireRow.Hidden = True


End Sub

You could try: 你可以尝试:

Option Explicit


Sub test()

    Dim arr As Variant, i As Long

    arr = Array(1254, 1276, 1299) '<- Create an array with all row you want to change

    For i = LBound(arr) To UBound(arr) '<- loop array

        Call Module1.Hide(arr(i)) '<- Call Hide sub

    Next i

End Sub

Sub Hide(ByVal Value As Long)

    Dim y As Long

    With ThisWorkbook.Worksheets("Sheet1")

         If Value = 1254 Then '<- Check value

            y = 22

        ElseIf Value = 1276 Then
            y = 23

        End If

        .Range(Value & ":" & Value + y).EntireRow.Hidden = True

    End With

End Sub

I'd create and maintain a named range that moves and shakes with your requirement. 我会创建并维护一个根据您的要求移动和摇动的命名范围。 You only need to include the cells in the first column given you're toggling the display of the entire row. 在切换整行的显示时,您只需要在第一列中包含单元格。

在此输入图像描述

From there, you can include that range in your code, you can see it in the Range call within the routine below. 从那里,您可以在代码中包含该范围,您可以在下面的例程中的Range调用中看到它。 If you then insert or delete rows, the shape of the named range automatically adjust. 如果随后插入或删除行,则命名范围的形状会自动调整。

You can also improve your routine to accept a boolean for show and then deal with it from a proxy routine for hide and show. 您还可以改进例程以接受show的布尔值,然后从代理例程处理它以进行隐藏和显示。

Sub WorkshopWork_HideMe()
    ToggleHideShow False
End Sub

Sub WorkshopWork_ShowMe()
    ToggleHideShow True
End Sub

Private Sub ToggleHideShow(ByVal bShow As Boolean)
    Application.ScreenUpdating = False

    ThisWorkbook.Sheets("Price calculation").Unprotect Password:="123"

    Range("ShowHideRange").EntireRow.Hidden = Not bShow

    ActiveSheet.Shapes("Rectangle: Rounded Corners 111").Visible = bShow
    ActiveSheet.Shapes("Rectangle: Rounded Corners 233").Visible = Not bShow

    ThisWorkbook.Sheets("Price calculation").Protect Password:="123"

    Application.ScreenUpdating = True
End Sub

Something to think about anyway. 无论如何要考虑的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM