在 Excel 中搜索重复项时是否可以排除隐藏行?
我正在 Excel 中使用 VBA 编写一个突出显示重复行的过程。该过程评估工作表函数 sumproduct 的结果以确定该行是否有重复项。
评估的公式最终看起来像这样:
SUMPRODUCT(--(A1:A10 = A1), --(B1:B10 = B1), --(C1:C10 = C1))
到目前为止,该过程运行良好,但我需要它忽略评估中的隐藏行和列。我可以使用 Range.Hidden = False 跳过循环中列中的隐藏行,但我还没有找到从 SUMPRODUCT 中排除隐藏行和列的方法。
我还尝试使用两个嵌套循环对每一行进行两次迭代,并且一次只比较两行的值,但这导致了 N 平方或 O(n2) 迭代,所以我放弃了就那个方法。
有没有办法强制 SUMPRODUCT 忽略隐藏行,就像电子表格公式 SUBTOTAL 一样?
这是我迄今为止使用 Evaluate(SUMPRODUCT)
得到的结果:谢谢!
Private Sub ShowDuplicateRows()
Dim lngRow As Long
Dim lngColumn As Long
Dim strFormula As String
With Selection
For lngRow = 1 To .Rows.Count
If Not .Rows(lngRow).Hidden Then
strFormula = "SUMPRODUCT("
For lngColumn = 1 To .Columns.Count
If Not .Columns(lngColumn).Hidden Then
If strFormula <> "SUMPRODUCT(" Then
strFormula = strFormula & ", "
End If
strFormula = strFormula _
& "--(" & .Columns(lngColumn).Address _
& " = " & .Cells(lngRow, lngColumn).Address & ")"
End If
Next
strFormula = strFormula & ")"
If Evaluate(strFormula) > 1 Then
.Rows(lngRow).Font.Color = RGB(255, 0, 0)
End If
End If
Next lngRow
End With
End Sub
I am working on a procedure in Excel using VBA that highlights duplicate rows. The procedure evaluates the result of the worksheet function sumproduct to determine if the row has duplicates.
The evaluated formula ends up looking like this:
SUMPRODUCT(--(A1:A10 = A1), --(B1:B10 = B1), --(C1:C10 = C1))
So far the procedure works great, but I need it to disregard hidden rows and columns from the evaluation. I can skip over hidden rows in columns in my loops using Range.Hidden = False
, but I haven't figured out a way to exclude hidden rows and columns from SUMPRODUCT.
I also tried iterating through every row twice using two nested loops and just comparing values two rows at a time, but that resulted in N-squared, or O(n2), iterations, so I gave up on that method.
Is there a way to coerce SUMPRODUCT into ignoring hidden rows, as is possible with the spreadsheet formula SUBTOTAL?
Here is what I have so far using Evaluate(SUMPRODUCT)
: Thanks!
Private Sub ShowDuplicateRows()
Dim lngRow As Long
Dim lngColumn As Long
Dim strFormula As String
With Selection
For lngRow = 1 To .Rows.Count
If Not .Rows(lngRow).Hidden Then
strFormula = "SUMPRODUCT("
For lngColumn = 1 To .Columns.Count
If Not .Columns(lngColumn).Hidden Then
If strFormula <> "SUMPRODUCT(" Then
strFormula = strFormula & ", "
End If
strFormula = strFormula _
& "--(" & .Columns(lngColumn).Address _
& " = " & .Cells(lngRow, lngColumn).Address & ")"
End If
Next
strFormula = strFormula & ")"
If Evaluate(strFormula) > 1 Then
.Rows(lngRow).Font.Color = RGB(255, 0, 0)
End If
End If
Next lngRow
End With
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RowHeight/Hidden 属性不会暴露给任何公式。解决方案必须是用 VBA 编写的。实现此目的的一种方法是创建一个用户定义的公式 (UDF) 来执行您想要的操作,然后在 sumproduct 公式中使用它。
那么你的公式将如下所示:
The RowHeight/Hidden property is not exposed to any formula. The solution will have to be in VBA. One way to accomplish this is to create a User Defined Formula (UDF) that does what you want it to, then just use it in your sumproduct formula.
Then your formula would look like this:
这是完整更新的代码。首先是主程序,然后是用户定义的函数。
如果有人能解释为什么将嵌套循环放入 UDF 中比将其放入主程序中更快,我将不胜感激!再次感谢奥朗!
我使 IsVisible UDF 的版本更加灵活。它可以使用作为参数传递的范围,或者如果没有传递,则使用
Application.Caller
。This is the full updated code. First the main procedure, then the user defined function.
If anyone can explain why putting the nested loop in a UDF is faster than having it in the main procedure I would greatly appreciated it! Thanks again to Oorang!
I made my version of the IsVisible UDF a bit more flexible. It can use a range passed as a parameter, or if none is passed it uses
Application.Caller
.