简体   繁体   English

根据单元格值隐藏多行的代码

[英]Code to hide multiple rows based on cell value

I am trying to check the cell value and based on that, 5 rows should be hidden.我正在尝试检查单元格值,并基于此,应该隐藏 5 行。

For example if the S4 cell value is <=0 then it should hide Rows from 4 to 8 and then again it should check S9 cell value will and if it is <=0 then it should hide rows from 9 to 13 and so on.例如,如果S4单元格值<=0 ,那么它应该隐藏行从4 到 8 ,然后它应该再次检查S9单元格值,如果它是<=0 ,那么它应该隐藏行从9 到 13等等。

Like this I have more than 1000 rows.像这样我有超过1000行。 Is there a better way handle this?有没有更好的方法来处理这个? Please let me know.请告诉我。

在此处输入图像描述

There is something called a "For loop" that helps you go through a big number of rows without having to write down every single one of them.有一种叫做“For循环”的东西可以帮助你通过大量的行go,而不必写下每一行。 Read about them here: https://www.excel-easy.com/vba/loop.html在这里阅读它们: https://www.excel-easy.com/vba/loop.html

I will give you a basic example:我会给你一个基本的例子:

Sub hideSomeRows()

Dim currentRow As Long
Dim lastRow As Long
lastRow = 1000 'or any other last row you want to check

For currentRow = 1 To lastRow Step 5
    If Range("S" & currentRow).Value <= 0 Then
        Rows(currentRow & ":" & currentRow + 4).EntireRow.Hidden = True
    End If
Next currentRow


End Sub

This will loop through every 5th row between row 1 and row 1000 and hide itself and the next 4 rows if there is a negative value or zero in column S of the inspected row.如果检查行的 S 列中存在负值或零,这将遍历第 1 行和第 1000 行之间的每 5 行,并隐藏自身和接下来的 4 行。

I kept the code as simple as possible, but you should really consider qualifying "Range" before using it.我使代码尽可能简单,但在使用它之前你真的应该考虑限定“范围”。 Here is a very good discussion of why it is important and how to do it. 是一个很好的讨论,为什么它很重要以及如何做到这一点。 Find some quick code snippets here showing how to define the workbook and the worksheet you are working on as variables in your code.在此处查找一些快速代码片段,展示如何将您正在处理的工作簿和工作表定义为代码中的变量。 And here is in-depth information about working with worksheets:以下是有关使用工作表的深入信息:

The gist of it: Do要点:做

Dim wb As Workbook
Set wb = ThisWorkbook

Dim ws As Worksheet
Set ws = wb.Sheets("!!YOURWORKSHEETNAME!!") 'replace !!YOURWORKSHEETNAME!! with the name of your worksheet

and then write ws.Range instead of just Range .然后写ws.Range而不是Range Same goes for Rows.行也是如此。 Why you ask?你为什么问? Try looking at another worksheet before executing your macro and you will see, that your macro is going to always interpret Range as a range of the last active worksheet if not told otherwise!在执行宏之前尝试查看另一个工作表,您会看到,如果没有另行说明,您的宏将始终将Range解释为最后一个活动工作表的范围!

As per your query I assume that you have 1000 rows and you have to hide next 4 rows if the value of the cell is <=0.根据您的查询,我假设您有 1000 行,如果单元格的值 <=0,则必须隐藏接下来的 4 行。

Sub Hide_Unhide()

Dim i As Integer

Application.ScreenUpdating = False

For i = 1 To 1000
    If (Not IsEmpty(ActiveSheet.Range("S" & i).Value)) And ActiveSheet.Range("S" & i).Value <= 0 Then
        ActiveSheet.Range("S" & i).EntireRow.Hidden = True
        i = i + 4
    End If
Next

Application.ScreenUpdating = True

End Sub

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

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