简体   繁体   English

根据 7 个工作表中的单元格值隐藏行 excel VBA

[英]Hide rows based on cell value across 7 sheets excel VBA

I searched the database to answer my question, but I couldn't find a similar situation.我搜索了数据库来回答我的问题,但我找不到类似的情况。 The first 4 worksheets will always be identical in terms of last names displayed and the exact same range/# of empty cells.前 4 个工作表在显示的姓氏和完全相同的范围/空单元格数量方面始终相同。 The last 3 worksheets will also have the same situation with the exact same range/# of empty cells, but the range will be different than sheets 1-4.最后 3 个工作表也将具有相同的情况,空单元格的范围/# 完全相同,但范围将与工作表 1-4 不同。 I am looking to write VBA to hide entire rows based on cell value in Column A for every sheet.我希望编写 VBA 以根据每个工作表的 A 列中的单元格值隐藏整行。 Cells in column A are populated with formulas. A 列中的单元格填充有公式。

Worksheets 1-4 Range A9-A58:工作表 1-4 范围 A9-A58:

Column A
Johnson             
Peterson
Anderson
(empty)
(empty)
(empty)

Worksheets 5-7 Range A16-A65:工作表 5-7 范围 A16-A65:

Column A
Johnson
Peterson
Anderson
(empty)
(empty)
(empty)

Result I am looking for all worksheets:结果我正在寻找所有工作表:

Column A
Johnson
Peterson
Anderson

Here is the code I've tried:这是我试过的代码:

Sub Macro1()
'
' Macro1 Macro
'


    Sheets("Sheet 1").Select
    Rows("19:58").Select
    Selection.EntireRow.Hidden = True
    Sheets("Sheet 2").Select
    Rows("19:58").Select
    Selection.EntireRow.Hidden = True
    Sheets("Sheet 3").Select
    Rows("19:58").Select
    Selection.EntireRow.Hidden = True
    Sheets("Sheet 4").Select
    Rows("19:58").Select
    Selection.EntireRow.Hidden = True
    Sheets("Sheet 5").Select
    Rows("26:65").Select
    Selection.EntireRow.Hidden = True
    Sheets("Sheet 6").Select
    Rows("26:65").Select
    Selection.EntireRow.Hidden = True
    Sheets("Sheet 7").Select
    Rows("26:65").Select
    Selection.EntireRow.Hidden = True
End Sub

The problem is I have to manually select the empty rows and go sheet by sheet to make it work.问题是我必须手动选择空行并逐张进行以使其工作。 It would be nice to have something to hide the empty rows based on cell value within the specific range of each sheet.根据每个工作表的特定范围内的单元格值隐藏空行会很好。

I am open to how the VBA is written whether its with screen updating or auto filtering.我对 VBA 的编写方式持开放态度,无论是通过屏幕更新还是自动过滤。 Thank you for your help/expertise!感谢您的帮助/专业知识!

Per my comment, will give you a starting place for how looping works.根据我的评论,将为您提供循环如何工作的起点。


First we'll lay-out some parameters... 7 sheets, 2 defined ranges.首先,我们将布置一些参数... 7 张纸,2 个定义的范围。 Let's start with the ranges:让我们从范围开始:

Dim aStart as Long, aStop as Long, bStart as Long, bStop as Long
aStart = 19
aStop = 58
bStart = 26
bStop = 65

So you've got some ranges outlined by their rows (a is first range, b is second range).所以你有一些由它们的行勾勒出的范围(a 是第一个范围,b 是第二个范围)。 Now what?怎么办? You want to look at each individual row between them, aka loop, to determine if it contains data or not, specifically in column A.您想查看它们之间的每一行,也就是循环,以确定它是否包含数据,特别是在 A 列中。

Dim i as Long  
For i = aStart to aStop
    If Cells( i, "A").Value = "" Then
        Rows(i).EntireRow.Hidden = True
    End If
Next i

Seems pretty simple, I hope.看起来很简单,我希望。 The loop uses i as the row, while looking at the iterating rows in column A, one at a time.循环使用 i 作为行,同时查看 A 列中的迭代行,一次一个。

This would look quite similar for bStart/bStop.对于 bStart/bStop,这看起来非常相似。 The only addition is making either happen... based on the sheet name/number.唯一的添加是根据工作表名称/编号使任何一种发生...。 There are umpteen ways for that to happen, and will leave it up to you to give it a whirl, as I've given you a lot so far, based on just recording a macro.有无数种方法可以实现这一点,这取决于你自己试一试,因为到目前为止我已经给了你很多,只是基于录制一个宏。

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

相关问题 Excel VBA代码(分配给按钮)基于多个工作表中的单元格值隐藏/取消隐藏行 - Excel VBA code (assigned to a button) to hide/unhide rows based on cell values across multiple sheets 如果单元格值为0,则excel vba切换隐藏/取消隐藏多个工作表中的范围行 - excel vba toggle hide/unhide range rows across multiple sheets, if cell value 0 如何创建函数以根据VBA EXCEL中的单元格值隐藏行 - How to create a function to hide rows based on a cell value in VBA EXCEL 使用Excel VBA隐藏基于单元格值的行 - Using Excel VBA To Hide Rows Based on Cell Value Excel VBA代码(分配给按钮)可基于另一个工作表中的单元格值隐藏/取消隐藏行 - Excel VBA code (assigned to a button) to hide/unhide rows based on cell values across another sheet 隐藏基于单元格值Excel VBA的命令按钮 - Hide commandbutton based on cell value excel vba 根据单元格值隐藏 Excel 中的行 - Hide Rows in Excel based on cell value 以更有效的方式根据单元格值删除两个不同工作表上的行 [VBA Excel] - Delete rows on two different sheets based on cell value in a more efficient way [VBA Excel] Excel宏可隐藏多个工作表中的行 - Excel Macro to hide rows across multiple sheets Excel VBA根据单元格颜色显示“隐藏行”,如果命令显示“是” - Excel VBA Hide Rows based on cell color and if command says “Yes”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM