简体   繁体   English

VBA根据多个单元格中的值隐藏/取消隐藏多行

[英]VBA to hide/unhide multiple rows based on values in multiple cells

Hoping someone may be able to identify what I am doing wrong.希望有人能够识别我做错了什么。 I've written VBA to hide/unhide rows based on values in multiple cells when button is clicked, but it's only working on the value in the first cell and not the full range.我编写了 VBA 来在单击按钮时根据多个单元格中的值隐藏/取消隐藏行,但它仅适用于第一个单元格中的值,而不是整个范围。

My file starts with rows 15:31 hidden.我的文件以隐藏的第 15:31 行开始。 The user is asked to submit their rating from 1 to 5 on rows 6:14 which the result feeds into column A of respective row, hence the case values in the code.要求用户在第 6:14 行提交他们从 1 到 5 的评分,结果馈入相应行的 A 列,因此代码中的案例值。 If any of the users ratings are less than 4, then rows 15:31 should remain hidden.如果任何用户评分小于 4,则第 15:31 行应保持隐藏。 If they are all 4 or over, then rows 15:22 should unhide.如果它们都是 4 或以上,则应取消隐藏第 15:22 行。 The user then rates themselves from 1 to 5 again on rows 15:22 and if their ratings are less than 4, rows 23:31 remain hidden but if they are all 4 or over, then rows 23:31 will unhide.然后,用户在第 15:22 行再次给自己评分,从 1 到 5,如果他们的评分小于 4,则第 23:31 行保持隐藏,但如果它们都为 4 或以上,则第 23:31 行将取消隐藏。

Here is my code:-这是我的代码:-

Sub Submit() 

    Select Case Range("A6,A7,A8,A9,A10,A11,A12,A13,A14") 

        Case Is >= 4: Rows("15:22").EntireRow.Hidden = False 

                      Worksheets("Print Result - Foundation").Visible = False 

            Select Case Range("A15,A16,A17,A18,A19,A20,A21,A22") 

                Case Is >= 4: Rows("23:31").EntireRow.Hidden = False 

                              Worksheets("Print Result - Grow").Visible = False 

                Case Is < 4: Rows("23:31").EntireRow.Hidden = True 

            End Select 

        Case Is < 4: Rows("15:31").EntireRow.Hidden = True 

    End Select 

End Sub

The above works well and hides rows 15:31 when A6 is less than 4 but when A7:A14 is less than 4, it displays rows 15:22.上面的效果很好,当 A6 小于 4 时隐藏第 15:31 行,但当 A7:A14 小于 4 时,它显示第 15:22 行。 And the same with the second range, when A15 is less than 4 rows 23:31 are hidden but when A16:A22 are less than 4, rows 23:31 are displaying.与第二个范围相同,当 A15 小于 4 行时,隐藏 23:31,但当 A16:A22 小于 4 时,显示行 23:31。

I have also tried entering the range as ("A6:A14") and ("A15:A22") but when I try to run the macro, it returns a run-time error '13': Type mismatch.我也尝试将范围输入为 ("A6:A14") 和 ("A15:A22") 但是当我尝试运行宏时,它返回运行时错误“13”:类型不匹配。

Can anyone see where I have gone wrong?谁能看到我哪里出错了?

I have a working solution in this instance.在这种情况下,我有一个可行的解决方案。 If you were to increase the number of rows that your user would input reviews, you would be required to update the range to include those values.如果您要增加用户输入评论的行数,则需要更新范围以包含这些值。 What I saw you went wrong with was what Ike had seen where your code only considered the first cell in the range rather than each cell in the range, so by looping through each cell for its value you can check each one.我看到你出错的是艾克所看到的,你的代码只考虑了范围内的第一个单元格而不是范围内的每个单元格,因此通过循环遍历每个单元格的值,你可以检查每个单元格。 I also used the loops rather than Select Case because, out of my own experience, I find it easier to sort by values using if conditionals because I only require one statement.我还使用了循环而不是 Select Case,因为根据我自己的经验,我发现使用 if 条件按值排序更容易,因为我只需要一个语句。

Sub Submit()
'
' Hide rows with low reviews
'
'Instantiate Ranges

Dim paramRange As Range
    Set paramRange = Range("A6:A14")
Dim condRange As Range
    Set condRange = Range("A15:A22")

'Instantiate counter
Dim i As Integer
    i = 0

'Hide rows before checking ratings
Rows("15:31").EntireRow.Hidden = True

'Iterate through each cell in the paramRange
For Each cell In paramRange

    'Check if rating is 4 or greater
    If cell.Value < 4 Then

        'If lower than 4 exit loop without increasing counter
        Exit For
    Else

    'If is 4 or greater add to counter
    i = i + 1
    End If
Next

'If counter is equal to number of cells in the paramRange
If i = paramRange.Count Then

    'Reveal rows 15:22
    Rows("15:22").EntireRow.Hidden = False

    'Reset counter
    i = 0

    'Iterate through each cell in the condRange
    For Each rating In condRange

        'Check if rating is 4 or greater
        If rating.Value < 4 Then
    
            'If lower than 4 exit loop without increasing counter
            Exit For
        Else
    
        'If is 4 or greater add to counter
        i = i + 1
        End If
    Next

    'If counter is equal to number of cells in the condRange
    If i = condRange.Count Then

        'Reveal rows 23:31
        Rows("23:31").EntireRow.Hidden = False
    End If
End If

'
End Sub

I hope that this answers your question and can help you out.我希望这能回答你的问题并能帮助你。 If not please let me know and I will keep working through it.如果没有,请告诉我,我会继续努力。 If better programmers than I find any way to make it work faster/more optimally that would be more helpful, but this works on my end to hide and unhide the selected rows.如果比我更好的程序员找到任何方法让它更快/更优化地工作,那将更有帮助,但这对我来说可以隐藏和取消隐藏选定的行。

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

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