Simplify my macro trigger so my table stops flickering?

Jeevz_87

New Member
Joined
Sep 21, 2021
Messages
37
Office Version
  1. 365
Platform
  1. Windows
Hi All,


I'm looking to simplify my macro which is triggered when a value within a drop down changes. The macro then finds all rows within a table that do not contain that value that matches the drop down and calls a macro to hide rows that do not match. As this process contains a few steps in it, it tends to cause the table to flicker momentarily whilst it filters the table view and I can assume that it has to do with the length of the macro process. My question is, it there any way of simplifying the macro below so that there's fewer steps and hopefully less 'flicker' when the macro is triggered?

Its a superficial thing to ask but I know the users will wonder if their laptops are having a melt-down for some reason.

Here's my code

VBA Code:
Private Sub worksheet_change(ByVal target As Range)
Set target = Range("A2")
If target.Value = "Location 1" Then
 Call HideRows
End If
If target.Value = "Location 2" Then
Call HideRows
End If
If target.Value = "Location 3" Then
Call HideRows
End If
If target.Value = "Location 4" Then
Call HideRows
End If
If target.Value = "Location 5" Then
Call HideRows
End If
If target.Value = "Location 6" Then
Call HideRows
End If
If target.Value = "Location 7" Then
'The macro call out below hides all rows in a hidden helper column in the table which have the value of '0'.
Call HideRows
End If
End Sub

To clarify, I want a way to simplify the above process so that it doesnt take as long to execute the filtering of the data within a table and hopefully reduce the flickering that occurs when the macro is triggered and executing the action.

Many thanks,


Jeevz
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
To stop the flickering use
VBA Code:
Sub subname()
Application.ScreenUpdating = False

' Code you want to run
Application.ScreenUpdating = True
End Sub
 
Upvote 0
VBA Code:
Private Sub worksheet_change(ByVal target As Range)
If Not Intersect(target, [A2]) Is Nothing Then
    Set target = [A2]
Else: Exit Sub
End If
Application.EnableEvents = False
If Left(target, 9) = "Location " & Right(target, 1) < 8 Then Call HideRows
Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
Revised :
VBA Code:
Private Sub worksheet_change(ByVal target As Range)
If Not Intersect(target, [A2]) Is Nothing Then
    Set target = [A2]
Else: Exit Sub
End If
Application.EnableEvents = False
If Left(target, 9) = "Location " And Right(target, 2) < 8 Then Call HideRows
Application.EnableEvents = True
End Sub
 
Upvote 0
As Target is the cell that has changed you should never try to set it to another range.
VBA Code:
Private Sub worksheet_change(ByVal Target As Range)
If Target.Address(0, 0) = "A2" Then
   Application.ScreenUpdating = False
   If Target.Value Like "Location*" And Right(Target.Value, 2) < 8 Then Call HideRows
End If
End Sub
 
Upvote 0
Solution
As Target is the cell that has changed you should never try to set it to another range.
VBA Code:
Private Sub worksheet_change(ByVal Target As Range)
If Target.Address(0, 0) = "A2" Then
   Application.ScreenUpdating = True
   If Target.Value Like "Location*" And Right(Target.Value, 2) < 8 Then Call HideRows
End If
End Sub
Except if the range being changed might consist of more cells than A2.
Also : Application.ScreenUpdating = True ?
VBA Code:
Private Sub worksheet_change(ByVal target As Range)
Dim r As Range
If Not Intersect(target, [A2]) Is Nothing Then
    Set r = [A2]
Else: Exit Sub
End If
Application.ScreenUpdating = False
If Left(r, 9) = "Location " And Right(r, 2) < 8 Then Call HideRows
End Sub
 
Last edited:
Upvote 0
Hi Everyone,

Thank you for your responses to this question. In fairness all the solutions here actually worked (I ran every one of the till i settled at one I preferred in this case) and the macro from @Kerryx did also stop the flickering.

Thank you for your help @footoo and @Fluff - One day I'll be as proficient as you :biggrin:😅

The issue I have is more to do with reading VBA as its different to reading formulae, dax and python code. Would you recommend any books that can at least help me better comprehend the VBA that I use?

Many thanks again everyone, apologies again for letting this dangle as long as I did.

Jeevz
 
Upvote 0
Glad we could help & thanks for the feedback.

I cannot recommend any books on VBA, as I have never read any of them.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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