首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Excel VBA隐藏行

Excel VBA隐藏行
EN

Stack Overflow用户
提问于 2021-12-13 06:02:20
回答 2查看 470关注 0票数 1

在下面的图片中,我试图隐藏特定单元格中空的行(例如39行到48行)。是否可以在一次点击中完成?我正计划和VBA一起做呢。

这是我目前使用的公式,但问题是,我想隐藏的单元格可能不会从第39行开始,或者在第48行结束,这取决于数据。

代码语言:javascript
运行
复制
Sub HideRows()
    Dim ws As Worksheet
    For Each ws In Worksheets(Array("NAMES", "AUGUST"))
        'ws.Rows("39:48").Hidden = True
Next
End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-13 11:38:22

隐藏“空”行

这是一种稍微不同的方法: cells

  • qualifies

  • 使用range

  • unhides常量,而变量

  • 对工作表和range

  • unhides all对象都使用For Each...Next循环(例如ws.Cells or rg.Cells,不只是Cells)

  • combines空单元格一次进入range

  • unhides all行,然后在另一个(go)

中隐藏“空”行)

代码语言:javascript
运行
复制
Option Explicit

Sub HideRows()

    Const StartRow As Long = 9
    Const EndRow As Long = 89
    Const ColNum As Long = 3
    Dim WorksheetNames As Variant
    WorksheetNames = Array("NAMES", "AUGUST") ' add more
    
    Dim ws As Worksheet ' Current Worksheet
    Dim rg As Range ' Current Range
    Dim hrg As Range ' Current Hide Range
    Dim cCell As Range ' Current Cell in Range
    
    ' Loop through the worksheets in the workbook containing this code.
    For Each ws In ThisWorkbook.Worksheets(WorksheetNames)
        ' Create a reference to the range of the current worksheet.
        Set rg = ws.Range(ws.Cells(StartRow, ColNum), ws.Cells(EndRow, ColNum))
        ' or using resize:
        'Set rg = ws.Cells(StartRow, ColNum).Resize(EndRow - StartRow + 1)
        ' Loop through the cells of the current range.
        For Each cCell In rg.Cells
            If IsEmpty(cCell) Then ' cell is empty
                ' Combine ('add') the current cell into the hide range.
                If Not hrg Is Nothing Then ' for all except the first
                    Set hrg = Union(hrg, cCell)
                Else ' for the first
                    Set hrg = cCell
                End If
            'Else ' cell is not empty - do nothing
            End If
        Next cCell
        ' Unhide all rows of the current range of the current worksheet.
        rg.EntireRow.Hidden = False
        If Not hrg Is Nothing Then ' there are combined cells
            ' Hide the rows of the hide range.
            hrg.EntireRow.Hidden = True
            ' Reset the hide range variable for the next worksheet.
            ' Also, note that 'Union' works only with ranges from one worksheet.
            Set hrg = Nothing
        'Else ' there are no combined cells - do nothing
        End If
    Next ws

End Sub
票数 1
EN

Stack Overflow用户

发布于 2021-12-13 06:41:48

我已经成功了。下面是我使用的脚本。

代码语言:javascript
运行
复制
Sub HideRows()

    Dim ws As Worksheet
    For Each ws In Worksheets(Array("NAMES", "AUGUST"))
    
        StartRow = 9
        EndRow = 89
        ColNum = 3
    
        For i = StartRow To EndRow
            If Not IsEmpty(Cells(i, ColNum).Value) Then
                ws.Cells(i, ColNum).EntireRow.Hidden = False
            Else
                ws.Cells(i, ColNum).EntireRow.Hidden = True
            End If
        Next i
    
    Next
End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70330509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档