Selection change breaks Filter

drag1c

Board Regular
Joined
Aug 7, 2019
Messages
92
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Im using this code to resize cell on click, to autofit text. Unfortunately, this code breaks filter and unfilitered data pops-up.
Does anyone have idea what could be done here so filter always remain the same until manually changed?
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Static iRow As Long

    Application.EnableEvents = True

    If Target.Row = 1 Then
    UsedRange.Rows.RowHeight = 12.75
    Exit Sub
    End If
 
    If Not Intersect(Target, Range("D:D")) Is Nothing Then
        If Target.Rows.Count > 1 Then Exit Sub
            If iRow > 0 Then
                Rows(iRow).RowHeight = 12.75
            End If
            iRow = Target.Row
            Target.EntireRow.AutoFit
            If Target.EntireRow.Height < 22 Then
                Rows(iRow).RowHeight = 24.75
            End If
    End If

    If Intersect(Target, Range("D:D")) Is Nothing Then
        If Target.Rows.Count > 1 Then Exit Sub
        If iRow > 0 Then
            UsedRange.Rows.RowHeight = 12.75
        End If
    End If

Application.EnableEvents = True
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
. Unfortunately, this code breaks filter and unfilitered data pops-up.
I tried to reproduce that issue but couldn't.

Try the following to see if it works for you:


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Static iRow As Long
  Application.EnableEvents = False
  
  If Target.Row = 1 Then
    UsedRange.SpecialCells(xlCellTypeVisible).Rows.RowHeight = 12.75
    Application.EnableEvents = True
    Exit Sub
  End If
  If Target.Rows.Count > 1 Then
    Application.EnableEvents = True
    Exit Sub
  End If
  
  If Not Intersect(Target, Range("D:D")) Is Nothing Then
    If iRow > 0 Then
      Rows(iRow).RowHeight = 12.75
    End If
    iRow = Target.Row
    Target.EntireRow.AutoFit
    If Target.EntireRow.Height < 22 Then
      Rows(iRow).RowHeight = 24.75
    End If
  Else
    If iRow > 0 Then
      UsedRange.SpecialCells(xlCellTypeVisible).Rows.RowHeight = 12.75
    End If
  End If
  
  Application.EnableEvents = True
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 1
Solution
Works like a charm. Thank you sir. I understand how you were thinking about it ☺

Please lock.
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,315
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top