【问题标题】:Excel VBA Hide Rows based on cell color and if command says "Yes"Excel VBA根据单元格颜色隐藏行,如果命令说“是”
【发布时间】:2017-03-06 13:36:22
【问题描述】:

我试图根据两个标准隐藏行:

标准 1:如果单元格 Q3 的值为“是”,则隐藏符合标准 2 的单元格

标准 2:如果 A 列中的单元格颜色为 RGB(253、233、217),则隐藏整行。

基本上,我有一个日期列表来跟踪每天的电子邮件计数,我想隐藏任何周末,这样它们就不会出现在显示趋势的图表上。我把它弄糊涂了我的上司,所以他们所要做的就是从单元格 Q3 的下拉菜单中单击“是”或“否”以隐藏周末行。周末是浅橙色(上面列出的 rgb 代码)。同样重要的是,如果单元格 Q3 状态为“否”,则所有行都将取消隐藏/保持不隐藏。我现在的代码是:

Sub HideRows()
BeginRow = 1
EndRow = 1000
ChkCol = 1
ChkCommCol = 17

For RowCnt = BeginRow To EndRow
    If Cells(RowCnt, ChkCommCol).Value = "Yes" Then
        If Cells(RowCnt, ChkCol) = RGB(253, 233, 217) Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        Else
            Cells(RowCnt, ChkCol).EntireRow.Hidden = False
    If Cells(RowCnt, ChkCol).EntireRow.Hidden = True Then
        Cells(RowCnt, ChkCol).EntireRow.Unhide = True
    End If
Next RowCnt

End Sub

如果您需要更多信息,请告诉我!非常感谢您的帮助。

【问题讨论】:

  • 如果您的代码不起作用,请描述任何错误及其发生位置。

标签: vba excel row show-hide


【解决方案1】:

首先,您在整个代码中遗漏了一些 End If 语句,我已经编辑了您的问题,并带有一些缩进以帮助显示这些语句在哪里,将来尝试缩进以帮助确保您关闭 @ 987654322@ 和 If 语句。

关于您的If 语句,您需要检查单元格内部的颜色,而不仅仅是单元格。这是通过语法完成的:

Cells(x, y).Interior.Color = RGB(r, g, b)

试试这个:

Sub HideRows()
BeginRow = 1
EndRow = 1000
ChkCol = 1
ChkCommCol = 17

Application.ScreenUpdating = False 'Speeds up subroutine
Application.Calculation = xlCalculationManual

If Cells(3, ChkCommCol).Value = "Yes" Then 'This line checks that `Q3` is "Yes"
    For RowCnt = BeginRow To EndRow 'This line loops through the rows and hides the weekends
        If Cells(RowCnt, ChkCol).Interior.Color = RGB(253, 233, 217) Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        End If
    Next RowCnt
Else
    Rows.EntireRow.Hidden = False 'This line unhides all rows in the activesheet if `Q3` isn't "Yes"
End If

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

【讨论】:

  • 感谢您的建议,我一定会努力使我的行正确缩进。我运行了代码,不幸的是,什么也没发生。我使用了我在网上找到的东西的顶部(beginrow、endrow 等),但我不知道这是否是我想做的正确表示法。代码没有错误,只是不会更改 excel 表中的任何内容。
  • 好的,再看看有几个问题,您实际检查Yes/No 的单元格实际上从BeginRow 增加到EndRow。我现在将编辑代码,因为我确信无论如何我们都可以将其减少一点。
  • 如果Q3No,您希望显示所有周末吗?
  • 是的,如果 Q3 说“不”,我希望所有周末都显示。
  • 现在编辑,只是为了确认一下,您正在检查单元格颜色的列当前是 ChkCol 的值,即 A 列,对吗?
【解决方案2】:

您可以在任何模块代码窗格中尝试此代码:

Sub HideRows()
    Dim beginRow As Long, endRow As Long, chkCol As Long, chkCommCol As Long, rowCnt As Long

    beginRow = 1
    endRow = cells(Rows.Count, 1).End(xlUp).Row '<--| set 'endRow' to column A last not empty cell row index
    chkCol = 1
    chkCommCol = 17

    Rows.EntireRow.Hidden = False 'unhides all rows. Subsequent code will hide relevant ones 

    If cells(3, chkCommCol).Value = "Yes" Then '<--| if Q3 value is "Yes"
        For rowCnt = beginRow To endRow '<--| loop through the "limit" rows indexes 
            With cells(rowCnt, chkCol) '<--| reference current cell to be cheked
                .EntireRow.Hidden = (.Interior.Color = RGB(253, 233, 217)) '<--| set its corresponding Row 'Hidden' property to True if currently referenced cell has wanted color
            End With
        Next
    End If
End Sub

并将以下代码放在相关的工作表代码窗格中:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$Q$3" Then HideRows '<--| if "Q3" cell has been changed then run 'HideRows' macro
End Sub

【讨论】:

  • 我很想告诉你这段代码是否成功;但是,当我将它输入模块并按下运行时,它开始了,但现在它不会停止。 VBE 一直在运行,excel 书本身在左下角一直在说“计算”。发生这种情况时,我无法选择或编辑任何单元格。我在第三季度说“是”时运行了这个,但我不知道这是否导致了这个。
  • 抱歉,我错误地输入了Worksheet_SelectionChange 而不是Worksheet_Change()。错过了.Row。请参阅编辑的代码。告诉我
  • 别担心,我想确保这不是我做错了什么。我添加了更改,它仍然只隐藏一行,我仍然需要手动运行宏。任何想法为什么当我更改代码时这没有改变?
  • 您必须遵循我给您的“说明”:第二个代码必须放在您的相关工作表代码窗格中(而不是在标准模块中),并且每次更改其 Q3 单元格都会触发宏.单步调试你的代码,看看它真的是这样的
  • 如果 sub 和 Application.ScreenUpdating=True 在其结尾之前,您可以在开头添加 Application.ScreenUpdating=False。
猜你喜欢
  • 1970-01-01
  • 2016-01-11
  • 2022-06-15
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
相关资源
最近更新 更多