Forum Discussion

style3body's avatar
style3body
Copper Contributor
Dec 02, 2021
Solved

VBA Code: count & hide

Dear All

I hope you are having a great day 

 

first, I would like to thank you all for your effort 

 

here is the system info : 

  • Device and OS platform, Windows 10 
  • Excel product name and version number 2013
  • Excel file you’re working with: Copy file 

 

--------------------------------

I have this Button in a sheet containing the following code : 

 

Figure 1

 

Sub ColorCompanyDuplicates()
'Updateby Extendoffice
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim xChar As String
    Dim xCellPre As Range
    Dim xCIndex As Long
    Dim xCol As Collection
    Dim I As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    xCIndex = 2
    Set xCol = New Collection
    For Each xCell In xRg
      On Error Resume Next
      xCol.Add xCell, xCell.Text
      If Err.Number = 457 Then
        xCIndex = xCIndex + 1
        Set xCellPre = xCol(xCell.Text)
        If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
        xCell.Interior.ColorIndex = xCellPre.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
        MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
        Exit Sub
      End If
      On Error GoTo 0
    Next
End Sub

 

basically, it highlights the duplicate values in the table, what I want is to add 2 other stuff to do in the button: 

 

  1. count how many duplicates are in column J and show the number in the yellow rectangle 
  2. Second, hide the yellow highlighted columns ( K, L, and from O to W)

 

 

Note: copy of the file attached 🙂  

 

Thank you in advance

 

 

10 Replies

Resources