Selection change breaks Filter

drag1c

Board Regular
Joined
Aug 7, 2019
Messages
97
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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
. 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,224,567
Messages
6,179,571
Members
452,927
Latest member
whitfieldcraig

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