Excel VBA:使用FormulaR1C1隐藏行

我试图隐藏包含假期date的行。 在另一个模块中,我在R3单元格中input“yes”来激活这个子模块,所以我只需要找出子模块。 没有错误,它只是不隐藏我需要隐藏的行。 我正在使用的公式可以find假期,因为当我将它放入条件格式时,它会正确地突出显示单元格。 代码如下:

Sub HideHolidays() Dim beginRow As Long, endRow As Long, chkCol As Long, chkCommCol As Long, rowCnt As Long Application.ScreenUpdating = False beginRow = 4 HolidaybeginRow = 2 endRow = ActiveWorkbook.Sheets("2017 All Districts").Cells(Rows.Count, 1).End(xlUp).Row '<--| set 'endRow' to column A last not empty cell row index chkCol = 1 chkCommCol = 17 chkHolCol = 18 'Set Sheets("2017 All Districts").Range(beginRow, chkCol).FormulaR1C1 = "=Match($A1,Holidays!$B$2:$B$11,0)" 'Rows.EntireRow.Hidden = False 'unhides all rows. Subsequent code will hide relevant ones If Cells(3, chkHolCol).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 If ActiveCell.FormulaR1C1 = "=Match($A1,Holidays!$B$2:$B$11,0)" Then Application.EntireRow.Hidden End If End With Next End If Application.ScreenUpdating = True End Sub 

样本数据 假日电子数据表引用

隐藏周末代码:Sub HideWeekends()Dim beginRow As Long,endRow As Long,chkCol As Long,chkCommCol As Long,rowCnt As Long

 Application.ScreenUpdating = False beginRow = 4 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 = Weekday(.Value2, vbMonday) > 5 '<--| set its corresponding Row 'Hidden' property to True if currently referenced cell has wanted color End With Next End If Application.ScreenUpdating = True End Sub 

一些问题:

  • 你使用ActiveCell.FormulaR1C1但是

    • ActiveCell既不是“更新”(有一些Select语句)

    • 也没有任何关系与实际循环variables是Cells(rowCnt, chkCol)

  • 你使用FormulaR1C1"=Match($A1,Holidays!$B$2:$B$11,0)"不在R1C1风格

  • ws.Cells(rowCnt, chkCol).FormulaR1C1 = "=Match($A1,Holidays!$B$2:$B$11,0)"您正在比较两个Formulaexpression式,而

    • 您选中单元格的实际内容是一个真正的date (不是返回date的公式)

    • 你要检查当前单元格的内容(一个date)是否在给定的范围内:你可以通过Range对象的Find()方法或Application对象的Match()方法来实现

  • 你没有限定所有范围,直到他们的工作表对象

对于上面的所有内容,你可以尝试一下你的代码的重构:

如果“2017年所有地区”表单“R3”单元格内容为“是”,则按照OP的要求进行编辑修改代码

 Sub HideHolidays() Dim beginRow As Long, rowCnt As Long, chkCol As Long beginRow = 4 chkCol = 1 With Worksheets("2017 All Districts") '<--| reference your sheet With .Range(.Cells(beginRow, chkCol), .Cells(.Rows.Count, chkCol).End(xlUp)) '<--| reference referenced sheet 'chkCol' column from row 'beginRow' down to last not empty one .EntireRow.Hidden = False 'unhides all referenced range rows: subsequent code will hide relevant ones if referenced sheet R3 cell isn't "Yes" If .Parent.Range("R3").Value <> "Yes" Then Exit Sub '<--| exit if referenced range sheet R3 cell value isn't "Yes" For rowCnt = 1 To .Rows.Count '<--| loop through all referenced range cells .Cells(rowCnt, 1).EntireRow.Hidden = Not IsError(Application.Match(CDbl(.Cells(rowCnt, 1)), Worksheets("Holidays").Range("$B$2:$B$11"), 0)) '<--| hide current cell entire row if its content doesn't match any value in "Holidays" sheet range $B$2:$B$11 Next End With End With End Sub 

您应该完全限定您要使用的对象。

 Sub HideHolidays() Dim beginRow As Long, endRow As Long, chkCol As Long, chkCommCol As Long, rowCnt As Long Dim ws As Worksheet Application.ScreenUpdating = False Set ws = ActiveWorkbook.Sheets("2017 All Districts") beginRow = 4 HolidaybeginRow = 2 endRow = ws.Cells(Rows.Count, 1).End(xlUp).Row '<--| set 'endRow' to column A last not empty cell row index chkCol = 1 chkCommCol = 17 chkHolCol = 18 'Set Sheets("2017 All Districts").Range(beginRow, chkCol).FormulaR1C1 = "=Match($A1,Holidays!$B$2:$B$11,0)" 'Rows.EntireRow.Hidden = False 'unhides all rows. Subsequent code will hide relevant ones If ws.Cells(3, chkHolCol).Value = "Yes" Then '<--| if Q3 value is "Yes" For rowCnt = beginRow To endRow '<--| loop through the "limit" rows indexes With ws.Cells(rowCnt, chkCol) '<--| reference current cell to be cheked If ws.Cells(rowCnt, chkCol).FormulaR1C1 = "=Match($A1,Holidays!$B$2:$B$11,0)" Then ws.Cells(rowCnt, chkCol).EntireRow.Hidden 'use the range object to hide and not application. End If End With Next End If Application.ScreenUpdating = True End Sub 

我build议你在你的代码表的顶部写Option Option,这样你将被迫声明所有的variables。 实际上,你有很多常量,而声明数值常量的最有效方法是通过一个枚举。 这就是我所做的。

 Option Explicit Private Enum Num FirstHolidayRow = 2 ChkRow ' no number declared means previous + 1 FirstRow chkCol = 1 chkCommCol = 17 ChkHolCol End Enum 

请注意,枚举必须放在代码片的顶部,在任何过程之前。 接下来我修改了你的代码。 现在看起来像这样:

 Sub HideHolidays() ' 24 Mar 2017 Dim WsAllDistricts As Worksheet Dim LastRow As Long Dim R As Long Set WsAllDistricts = ActiveWorkbook.Sheets("2017 All Districts") Application.ScreenUpdating = False With WsAllDistricts '<--| set 'endRow' to column A last not empty cell row index LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row End With With WsAllDistricts If .Cells(ChkRow, ChkHolCol).Value = "Yes" Then '<--| if Q3 value is "Yes" For R = FirstRow To LastRow '<--| loop through the "limit" rows indexes If InStr(1, .Cells(R, chkCol).Formula, _ "Holidays!$B$2:$B$11,0)", vbTextCompare) Then .Rows(R).EntireRow.Hidden = True End If Next R End If End With Application.ScreenUpdating = True End Sub 

它的工作原理,但我不确定它是否做你想做的事情。 推定是只有一张纸。 如果有另一个它不出现在你的代码。 请记住,您必须指定工作簿来查找工作表,find一行的工作表以及查找单元格的列。 有时这些规范是隐含的,就像一张表格完全知道它所属的工作簿一样,但Range(“A1”)不是这种情况,可以在任何表单上。 当然,如果你想要解决ActiveCell,你必须先激活一个单元。 所有的寻址差异都被解决了。

因为公式的语法=MATCH($A9 ,其中第9行是可变的),所以你需要查找公式,所以在你的工作表中只有一个公式是这样的。只看比赛范围"Holidays!$B$2:$B$11,0)" 。 如果这还不够,还有其他方法可以更精确地处理这个问题。