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

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

Hide the visible duplicate cells by using a helper column

问题

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

    Function Hide_visible_duplicate_cells(procRng As Range)
     
        Dim arng As Range, rngU As Range, C As Range, dict As New Scripting.Dictionary
     
        Set arng = procRng.SpecialCells(xlCellTypeVisible)
     
        If arng Is Nothing Then MsgBox "Not a valid Range": Exit Function
     
        For Each C In arng.Cells
            If Not dict.Exists(C.Value) Then
                dict.Add C.Value, vbNullString       'Keep the first occurrence
            Else
              If rngU Is Nothing Then                'Create a Union range for the next occurrences:
                 Set rngU = C
              Else
                 Set rngU = Union(rngU, C)
              End If
            End If
        Next C
     
       If Not rngU Is Nothing Then rngU.EntireRow.Hidden = True    'Hide the rows at once
     
    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>

Function Hide_visible_duplicate_cells(procRng As Range)
 
    Dim arng As Range, rngU As Range, C As Range, dict As New Scripting.Dictionary
 
    Set arng = procRng.SpecialCells(xlCellTypeVisible)
 
    If arng Is Nothing Then MsgBox &quot;Not a valid Range&quot;: Exit Function
 
    For Each C In arng.Cells
        If Not dict.Exists(C.value) Then
            dict.Add C.value, vbNullString       &#39;Keep the first occurrence
        Else
          If rngU Is Nothing Then                &#39;Create a Union range for the next occurrences:
             Set rngU = C
          Else
             Set rngU = Union(rngU, C)
          End If
        End If
    Next C
 
   If Not rngU Is Nothing Then rngU.EntireRow.Hidden = True    &#39;Hide the rows at once
 
End Function

答案1

得分: 1

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

Sub Hide_visible_duplicate_cells_(procRng As Range)
    Dim arng As Range, C As Range, dict As New Scripting.Dictionary
    Dim arrMark, colMark As Range, lastC As Long, sh As Worksheet, lastR As Long, i As Long
    
    Const markName As String = "Marker_column"
    
    Set arng = procRng.SpecialCells(xlCellTypeVisible)
    
    If arng Is Nothing Then MsgBox "Not a valid Range": Exit Sub
    
    Set sh = procRng.Parent 'the sheet where the range belongs to

    lastR = sh.UsedRange.Rows(sh.UsedRange.Rows.Count).Row 'last row OF THE SHEET
    ReDim arrMark(1 To lastR, 1 To 1) 'redim the markers array
    
    'determine the column where the marker to be placed (or it already exists):
    Set colMark = sh.Rows(procRng.Cells(1).Row).Find(What:=markName, LookIn:=xlValues, LookAt:=xlWhole)
    If Not colMark Is Nothing Then
        lastC = colMark.Column 'for the case when the marker column exists
    Else
        lastC = sh.Cells(procRng.Cells(1).Row, sh.Columns.Count).End(xlToLeft).Column + 1 'next empty column if marker column does not exist
        'to correct the last column number, IF LAST COLUMN IS HIDDEN (it MUST HAVE A HEADER):
        If sh.Cells(procRng.Cells(1).Row, lastC).Value <> "" Then lastC = lastC + 1
    End If
    
    For Each C In arng.Cells
        If Not dict.Exists(C.Value) Then
            If i > 0 Then                                            'to skip the first cell, which should be on the headers row
                dict.Add C.Value, vbNullString       'Keep the first occurrence
                arrMark(C.Row - procRng.Cells(1).Row, 1) = "True"      'place the marker for the first occurrence
            End If
            If C.Value <> "" Then i = i + 1 'for the case of empty cells above the header...
        End If
    Next C
    'place the marker column header, if not already existing:
    If colMark Is Nothing Then sh.Cells(procRng.Cells(1).Row, lastC).Value = markName 'place the marker column name, IF NOT EXISTS
    
    If sh.AutoFilterMode Then sh.AutoFilterMode = False  'eliminate the filter, if any
    
    'drop the markers array content:
    sh.Cells(procRng.Cells(1).Row + 1, lastC).Resize(UBound(arrMark), 1).Value2 = arrMark
    
    'filter by the marker column
    sh.Range(sh.Cells(procRng.Cells(1).Row, 1), sh.Cells(sh.UsedRange.Rows.Count, lastC)).AutoFilter lastC, "True"
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:

Sub Hide_visible_duplicate_cells_(procRng As Range)
    Dim arng As Range, C As Range, dict As New Scripting.Dictionary
    Dim arrMark, colMark As Range, lastC As Long, sh As Worksheet, lastR As Long, i As Long
    
    Const markName As String = &quot;Marker_column&quot;
    
    Set arng = procRng.SpecialCells(xlCellTypeVisible)
    
    If arng Is Nothing Then MsgBox &quot;Not a valid Range&quot;: Exit Sub
    
    Set sh = procRng.Parent &#39;the sheet where the range belongs to

    lastR = sh.UsedRange.rows(sh.UsedRange.rows.count).row  &#39;last row OF THE SHEET
    ReDim arrMark(1 To lastR, 1 To 1) &#39;redim the markers array
    
    &#39;determinte the column where the marker to be placed (or it already exists):
    Set colMark = sh.rows(procRng.cells(1).row).Find(What:=markName, LookIn:=xlValues, LookAt:=xlWhole)
    If Not colMark Is Nothing Then
        lastC = colMark.column  &#39;for the case when the marker column exists
    Else
        lastC = sh.cells(procRng.cells(1).row, sh.Columns.count).End(xlToLeft).column + 1 &#39;next empty column if marker column does not exist
        &#39;to correct the last column number, IF LAST COLUMN IS HIDDEN (it MUST HAVE A HEADER):
        If sh.cells(procRng.cells(1).row, lastC).Value &lt;&gt; &quot;&quot; Then lastC = lastC + 1
    End If
    
    For Each C In arng.cells
        If Not dict.Exists(C.Value) Then
            If i &gt; 0 Then                                            &#39;to skip the first cell, which should be on the headers row
                dict.Add C.Value, vbNullString       &#39;Keep the first occurrence
                arrMark(C.row - procRng.cells(1).row, 1) = &quot;True&quot;      &#39;place the marker for the first occurrence
            End If
            If C.Value &lt;&gt; &quot;&quot; Then i = i + 1 &#39;for the case of empty cells above the header...
        End If
    Next C
    &#39;place the marker column header, if not already existing:
     If colMark Is Nothing Then sh.cells(procRng.cells(1).row, lastC).Value = markName &#39;place the marker column name, IF NOT EXISTS
     
    If sh.AutoFilterMode Then sh.AutoFilterMode = False  &#39;eliminate the filter, if any
    
    &#39;drop the markers array content:
    sh.cells(procRng.cells(1).row + 1, lastC).Resize(UBound(arrMark), 1).Value2 = arrMark
    
    &#39;filter by the marker column
    sh.Range(sh.cells(procRng.cells(1).row, 1), sh.cells(sh.UsedRange.rows.count, lastC)).AutoFilter lastC, &quot;True&quot;
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.

=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:

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

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:

确定