使用辅助列隐藏可见的重复单元格。

huangapple go评论104阅读模式
英文:

Hide the visible duplicate cells by using a helper column

问题

下面的代码用于隐藏可见的重复单元格(行)。 <br>
它有效,但是如果我稍后在任何列上使用自动筛选,那么所有隐藏的行将再次显示。 <br>
我的目标是使用辅助列并在值上设置过滤器,例如(False)。 <br>
提前感谢任何帮助。 <br>

  1. Function Hide_visible_duplicate_cells(procRng As Range)
  2. Dim arng As Range, rngU As Range, C As Range, dict As New Scripting.Dictionary
  3. Set arng = procRng.SpecialCells(xlCellTypeVisible)
  4. If arng Is Nothing Then MsgBox "Not a valid Range": Exit Function
  5. For Each C In arng.Cells
  6. If Not dict.Exists(C.Value) Then
  7. dict.Add C.Value, vbNullString 'Keep the first occurrence
  8. Else
  9. If rngU Is Nothing Then 'Create a Union range for the next occurrences:
  10. Set rngU = C
  11. Else
  12. Set rngU = Union(rngU, C)
  13. End If
  14. End If
  15. Next C
  16. If Not rngU Is Nothing Then rngU.EntireRow.Hidden = True 'Hide the rows at once
  17. End Function
英文:

The below code is used to hide the visible duplicate cells (rows). <br>
It works , But If I later used autofiletr on any column then all the hidden rows are shown again. <br>
My aim is to use a helper column and set filter on value e,g (False). <br>
In advance, thanks for any help. <br>

  1. Function Hide_visible_duplicate_cells(procRng As Range)
  2. Dim arng As Range, rngU As Range, C As Range, dict As New Scripting.Dictionary
  3. Set arng = procRng.SpecialCells(xlCellTypeVisible)
  4. If arng Is Nothing Then MsgBox &quot;Not a valid Range&quot;: Exit Function
  5. For Each C In arng.Cells
  6. If Not dict.Exists(C.value) Then
  7. dict.Add C.value, vbNullString &#39;Keep the first occurrence
  8. Else
  9. If rngU Is Nothing Then &#39;Create a Union range for the next occurrences:
  10. Set rngU = C
  11. Else
  12. Set rngU = Union(rngU, C)
  13. End If
  14. End If
  15. Next C
  16. If Not rngU Is Nothing Then rngU.EntireRow.Hidden = True &#39;Hide the rows at once
  17. End Function

答案1

得分: 1

请,尝试下一种方法。但您需要使用需要处理的列范围,注意其第一行必须是列标题:

  1. Sub Hide_visible_duplicate_cells_(procRng As Range)
  2. Dim arng As Range, C As Range, dict As New Scripting.Dictionary
  3. Dim arrMark, colMark As Range, lastC As Long, sh As Worksheet, lastR As Long, i As Long
  4. Const markName As String = "Marker_column"
  5. Set arng = procRng.SpecialCells(xlCellTypeVisible)
  6. If arng Is Nothing Then MsgBox "Not a valid Range": Exit Sub
  7. Set sh = procRng.Parent 'the sheet where the range belongs to
  8. lastR = sh.UsedRange.Rows(sh.UsedRange.Rows.Count).Row 'last row OF THE SHEET
  9. ReDim arrMark(1 To lastR, 1 To 1) 'redim the markers array
  10. 'determine the column where the marker to be placed (or it already exists):
  11. Set colMark = sh.Rows(procRng.Cells(1).Row).Find(What:=markName, LookIn:=xlValues, LookAt:=xlWhole)
  12. If Not colMark Is Nothing Then
  13. lastC = colMark.Column 'for the case when the marker column exists
  14. Else
  15. lastC = sh.Cells(procRng.Cells(1).Row, sh.Columns.Count).End(xlToLeft).Column + 1 'next empty column if marker column does not exist
  16. 'to correct the last column number, IF LAST COLUMN IS HIDDEN (it MUST HAVE A HEADER):
  17. If sh.Cells(procRng.Cells(1).Row, lastC).Value <> "" Then lastC = lastC + 1
  18. End If
  19. For Each C In arng.Cells
  20. If Not dict.Exists(C.Value) Then
  21. If i > 0 Then 'to skip the first cell, which should be on the headers row
  22. dict.Add C.Value, vbNullString 'Keep the first occurrence
  23. arrMark(C.Row - procRng.Cells(1).Row, 1) = "True" 'place the marker for the first occurrence
  24. End If
  25. If C.Value <> "" Then i = i + 1 'for the case of empty cells above the header...
  26. End If
  27. Next C
  28. 'place the marker column header, if not already existing:
  29. If colMark Is Nothing Then sh.Cells(procRng.Cells(1).Row, lastC).Value = markName 'place the marker column name, IF NOT EXISTS
  30. If sh.AutoFilterMode Then sh.AutoFilterMode = False 'eliminate the filter, if any
  31. 'drop the markers array content:
  32. sh.Cells(procRng.Cells(1).Row + 1, lastC).Resize(UBound(arrMark), 1).Value2 = arrMark
  33. 'filter by the marker column
  34. sh.Range(sh.Cells(procRng.Cells(1).Row, 1), sh.Cells(sh.UsedRange.Rows.Count, lastC)).AutoFilter lastC, "True"
  35. End Sub

这是您提供的VBA宏的翻译。如果您有任何其他问题或需要进一步的帮助,请随时提问。

英文:

Please, try the next way. But you need to use the column range which you need to be processed, taking care that its first row to be the column headers one:

  1. Sub Hide_visible_duplicate_cells_(procRng As Range)
  2. Dim arng As Range, C As Range, dict As New Scripting.Dictionary
  3. Dim arrMark, colMark As Range, lastC As Long, sh As Worksheet, lastR As Long, i As Long
  4. Const markName As String = &quot;Marker_column&quot;
  5. Set arng = procRng.SpecialCells(xlCellTypeVisible)
  6. If arng Is Nothing Then MsgBox &quot;Not a valid Range&quot;: Exit Sub
  7. Set sh = procRng.Parent &#39;the sheet where the range belongs to
  8. lastR = sh.UsedRange.rows(sh.UsedRange.rows.count).row &#39;last row OF THE SHEET
  9. ReDim arrMark(1 To lastR, 1 To 1) &#39;redim the markers array
  10. &#39;determinte the column where the marker to be placed (or it already exists):
  11. Set colMark = sh.rows(procRng.cells(1).row).Find(What:=markName, LookIn:=xlValues, LookAt:=xlWhole)
  12. If Not colMark Is Nothing Then
  13. lastC = colMark.column &#39;for the case when the marker column exists
  14. Else
  15. lastC = sh.cells(procRng.cells(1).row, sh.Columns.count).End(xlToLeft).column + 1 &#39;next empty column if marker column does not exist
  16. &#39;to correct the last column number, IF LAST COLUMN IS HIDDEN (it MUST HAVE A HEADER):
  17. If sh.cells(procRng.cells(1).row, lastC).Value &lt;&gt; &quot;&quot; Then lastC = lastC + 1
  18. End If
  19. For Each C In arng.cells
  20. If Not dict.Exists(C.Value) Then
  21. If i &gt; 0 Then &#39;to skip the first cell, which should be on the headers row
  22. dict.Add C.Value, vbNullString &#39;Keep the first occurrence
  23. arrMark(C.row - procRng.cells(1).row, 1) = &quot;True&quot; &#39;place the marker for the first occurrence
  24. End If
  25. If C.Value &lt;&gt; &quot;&quot; Then i = i + 1 &#39;for the case of empty cells above the header...
  26. End If
  27. Next C
  28. &#39;place the marker column header, if not already existing:
  29. If colMark Is Nothing Then sh.cells(procRng.cells(1).row, lastC).Value = markName &#39;place the marker column name, IF NOT EXISTS
  30. If sh.AutoFilterMode Then sh.AutoFilterMode = False &#39;eliminate the filter, if any
  31. &#39;drop the markers array content:
  32. sh.cells(procRng.cells(1).row + 1, lastC).Resize(UBound(arrMark), 1).Value2 = arrMark
  33. &#39;filter by the marker column
  34. sh.Range(sh.cells(procRng.cells(1).row, 1), sh.cells(sh.UsedRange.rows.count, lastC)).AutoFilter lastC, &quot;True&quot;
  35. End Sub

答案2

得分: 0

=COUNTIF(A$1:A2,A2)=1

(Note that on my computer the parameter separator is a semicolon)

if you want to write this value via VBA:

If Not dict.Exists(C.value) Then
dict.Add C.value, vbNullString ' Keep the first occurrence
C.Offset(0, 1).value = True ' Signal: This is the first occurrence
Else
C.Offset(0, 1).value = False
(...)

英文:

You can easily create such helper column with a formula. Put the following formula into your helper column. The example assumes that the values that needs to be checked for duplicates are in column A and that your data starts in row 2.

  1. =COUNTIF(A$1:A2,A2)=1

使用辅助列隐藏可见的重复单元格。

(Note that on my computer the parameter separator is a semicolon)

Update

if you want to write this value via VBA:

  1. If Not dict.Exists(C.value) Then
  2. dict.Add C.value, vbNullString &#39; Keep the first occurrence
  3. C.Offset(0, 1).value = True &#39; Signal: This is the first occurrence
  4. Else
  5. C.Offset(0, 1).value = False
  6. (...)

You just need to adapt the Offset so that the data is written into the correct column.

huangapple
  • 本文由 发表于 2023年3月7日 22:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定