Slow vba code

Even

Board Regular
Joined
Jan 1, 2013
Messages
81
Office Version
  1. 365
Platform
  1. Windows
Hi, this code is very slow, and I don't know why. It is a dropdown list in B1. And whenever I choose a name, it takes long time to calculate. Anybody?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer

Application.ScreenUpdating = False
If Not Application.Intersect(Range("B1"), Range(Target.Address)) Is Nothing Then
    Select Case Target.Value
    
    Case Is = "Text":
        For i = 3 To 500
            If Range("AK" & i).Value = "Text" Then
            Rows(i).EntireRow.Hidden = False
            Else: Rows(i).EntireRow.Hidden = True
        End If

    Next i
    
    Case Is = "Text":
        For i = 3 To 500
            If Range("AK" & i).Value = "Text" Then
            Rows(i).EntireRow.Hidden = False
            Else: Rows(i).EntireRow.Hidden = True
        End If
        
        
    
        Application.ScreenUpdating = True

 End Select

    
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Why do you have the exact same block of code repeated twice?
Do you have any other Event Procedure VBA code running?

Also, note that this:
VBA Code:
Range(Target.Address)
can be replaced with just this:
VBA Code:
Target
as "Target" is already a range variable.

There is no point in taking a range, getting the address of it, and then making it a range again.
Just use the original range!
 
Upvote 0
I'm surprised this code works at all. Is there possibly something missing from what you posted?
 
Upvote 0
Instead of looping through the rows and hiding if AK's value is "Text", why not just autofilter column AK for "Text"? Autofilter will be significantly faster than your loop...
 
Upvote 0
Thank you guys. These are all very good questions to which I don't have answers :) You see, I had help with the code :) The "Text" is different in each one. I just wrote text for simplicity. I will tweak around with above suggestions. Regarding the autofilter, I am not familiar to how to do that in a code.
 
Upvote 0
Joe4: After removing the range, I guess it became a little faster. But it's not Speedy Gonzales :)
 
Upvote 0
Joe4: After removing the range, I guess it became a little faster. But it's not Speedy Gonzales
That doesn't make it any faster, just a note on some unnecessary stuff that you had in there.

How many different values are you looking for?
Loops are notoriously slow. If you are looping through the entire range fully for each one, that would be pretty slow. There are faster ways of doing it, or at least of limiting the number of loops you have.

But we need to fully understand the scope here:
- How many values are you really looking for?
- Are these values hard-coded (always the same), or are they coming from somewhere else?
- How many rows of data do you really have? One way to limit the number of rows you have to loop through is to dynamically find the last row of data, i.e.
VBA Code:
lastRow = Cells(Rows.Count,"AK").End(xlUp).Row
For i = 1 to lastRow
    ...
 
Upvote 0
I don't see why your code should be exceptionally slow, not for just under 500 rows. However, your code can be compacted as follows (not sure this will be noticeably faster, just more compact)...
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim R As Long
  Application.ScreenUpdating = False
  If Target.Address(0, 0) = "B1" Then
    For R = 3 To 500
      Rows(R).Hidden = (Cells(R, "AK").Value = Range("B1").Value)
    Next
  End If
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Rick: That was so much more compact :) But it didn't perform what I needed. I might need to give more information. Pls stay tuned :)
 
Upvote 0
Answering the questions I asked in the previous task, and providing some sample data may go a long way in helping us devising a solution that will work for you.
 
Upvote 0

Forum statistics

Threads
1,214,626
Messages
6,120,602
Members
448,974
Latest member
ChristineC

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