简体   繁体   English

根据单元格中的值隐藏和取消隐藏一系列行

[英]Hide and Unhide a range of rows based on the value in a cell

Very Basic I am sure but I can't figure it out for the life of me.非常基本,我敢肯定,但我一生都无法弄清楚。

I have a set of radio buttons that changes the value of cell("L37") between 1 and 2.我有一组单选按钮,可以在 1 和 2 之间更改单元格(“L37”)的值。

I have tried to write the VBA code several different ways without luck.我尝试用几种不同的方式编写 VBA 代码,但没有运气。 Please advise.请指教。

Sub hide_sheet()
    If Worksheets("Feedback").Range("L37").Value = 1 Then
      Rows("63:93").EntireRow.Hidden = True
    Else Worksheets("Feedback").Range("L37").Value = 2 Then
      Worksheets("Feedback").Rows("63:93").EntireRow.Hidden = False
    End If
End Sub

I had it working perfectly fine via a Macro tied to the radio buttons;我通过与单选按钮绑定的宏让它工作得很好; however, due to me needing to protect the sheet I needed to change it.但是,由于我需要保护工作表,我需要更改它。

I am using a protect/unprotect VBA code and would like to include it into the code so it won't set off the macro/sheet protected warning.我正在使用保护/取消保护 VBA 代码,并希望将其包含到代码中,这样它就不会触发宏/工作表保护警告。

Here is my protect/unprotect code I am using for everything.这是我用于所有内容的保护/取消保护代码。

Sub unprotect()

    Worksheets("Feedback").unprotect
    
End Sub

Sub protect()

    Worksheets("Feedback").protect , _
        AllowFormattingCells:=True, _
        AllowFormattingRows:=True
        
    
End Sub

Any advice would be greatly appreciated.任何建议将不胜感激。 I Thank You in Advance of your assistance.提前感谢您的帮助。

Show/Hide Rows显示/隐藏行

Issues问题

  • The Feedback worksheet is not qualified so if the wrong workbook is active, it will fail. Feedback工作表不合格,因此如果错误的工作簿处于活动状态,它将失败。 To reference the workbook containing this code, you can use ThisWorkbook :要引用包含此代码的工作簿,您可以使用ThisWorkbook

     ThisWorkbook.Worksheets("Feedback")...
  • You are using Rows("63:93") instead of Worksheets("Feedback").Rows("63:93") in the If clause.您在 If 子句中使用Rows("63:93")而不是Worksheets("Feedback").Rows("63:93") If the wrong worksheet is active (selected), it will fail.如果错误的工作表处于活动状态(选中),它将失败。

  • You are using Else instead of ElseIf .您正在使用Else而不是ElseIf

  • You can use the With statement to reduce typing as illustrated in the following code.您可以使用With语句来减少键入,如以下代码所示。

  • If you convert the cell value to a string, then if the cell accidentally contains an error value, the code will not fail.如果将单元格值转换为字符串,则如果单元格意外包含错误值,则代码不会失败。

The Code编码

Sub ShowHideRowsFix()
    With ThisWorkbook.Worksheets("Feedback")
        .Unprotect
        Select Case CStr(.Range("L37").Value)
        Case "1"
            .Rows("63:93").Hidden = True
        Case "2"
            .Rows("63:93").Hidden = False
        Case Else
        End Select
        .Protect AllowFormattingCells:=True, AllowFormattingRows:=True
    End With
End Sub

An Improvement一种提升

  • To automate this operation (no need for buttons), in the sheet module of the Feedback worksheet identified in the VBE Project explorer window by eg Sheet1(FeedBack) (double-click to open), you could use the following code.要自动执行此操作(不需要按钮),在 VBE 项目浏览器 window 中标识的Feedback工作表的工作表模块中,例如Sheet1(FeedBack) (双击打开),您可以使用以下代码。
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const CellAddress As String = "L37"
    
    Dim Cell As Range: Set Cell = Me.Range(CellAddress)
    If Intersect(Cell, Target) Is Nothing Then Exit Sub
    
    ShowHideRows Cell

End Sub

Sub ShowHideRows(ByVal Cell As Range)
    With Cell.Worksheet
        .Unprotect
        Select Case CStr(Cell.Value)
        Case "1"
            .Rows("63:93").Hidden = True
        Case "2"
            .Rows("63:93").Hidden = False
        Case Else
        End Select
        .Protect AllowFormattingCells:=True, AllowFormattingRows:=True
    End With
End Sub

I prefer the simplest approach where possible.我更喜欢尽可能简单的方法。 This is the approach I use to hide columns re-written for your needs.这是我用来隐藏为您的需要重新编写的列的方法。

The following code may exist on the UserForm or in a standard Code Module: 以下代码可能存在于用户窗体或标准代码模块中:

IF RadioButton1.Value = True then 'Assuming the value is 1
   rwControl "hRW"   'This hides the Rows
 ELSE
   rwControl "uRW"   'This unhides the Rows
End If

You may call this code directly from you RadioButton_Change() Event vy including the following code:您可以直接从 RadioButton_Change() 事件中调用此代码,包括以下代码:

 IF RadioButton1.Value = True then 'Assuming the value is 1 rwControl "hRW" 'This hides the Rows ELSE rwControl "uRW" 'This unhides the Rows End If

OR , to keep it really simple:或者,为了保持简单:

in your RadioButton_Change() event simply add: 在您的RadioButton_Change()事件中只需添加:

 Private Sub RadioButton1 Change() With ThisWorkBook.Worksheets("Feedback").Unprotect If RadioButton1.Value = True Then.Rows("63:93").Hidden = True Else.Rows("63:93").Hidden = False End If.Protect End With End Sub

Using this approach negates the need for Worksheet Module Coding and Case Coding and allows you to keep the RadioButtons if you deem them important to the functionality.使用这种方法可以消除对Worksheet Module CodingCase Coding的需求,并且如果您认为RadioButtons对功能很重要,则可以保留它们。

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

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