Macro is slow

Deepk

Board Regular
Joined
Mar 21, 2018
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

find below a code for coloring cell based on the adjacent cell value. This code is running very slow, especially if the data in selection is more than 2000. Please help.

Code:
Private Sub CommandButton2_Click()
 Dim c As Range


    firstVal = 0
    cellColor = 0
       
  With Selection
    
    If Selection.Rows.Count > 100000 Then
      MsgBox Prompt:="Please verify the data you have selected", Title:="Wrong selection!"
      Exit Sub
    Else
      For Each c In Selection
        If c.Interior.Color <> cellColor Then
            firstVal = firstVal + 1
            cellColor = c.Interior.Color
     End If
        Cells(c.Row, c.Column - 1).Value = firstVal
      Next c
    End If
  End With
    MsgBox "Process Completed Successfully."
    Unload UserForm2
End Sub
 
Last edited by a moderator:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I only see one small thing you could change. I changed it and marked it in red.
Please define "Slow" do you mean this takes 3 minutes or 3 seconds.

Code:
Private Sub CommandButton2_Click()
 Dim c As Range

    firstVal = 0
    cellColor = 0
       
  With Selection
    
    If Selection.Rows.Count > 100000 Then
      MsgBox Prompt:="Please verify the data you have selected", Title:="Wrong selection!"
      Exit Sub
    Else
      For Each c In Selection
        If c.Interior.Color <> cellColor Then
            firstVal = firstVal + 1
            cellColor = c.Interior.Color
     End If
       [COLOR=#ff0000] 'Cells(c.Row, c.Column - 1).Value = firstVa  old linel
      c.Offset(, -1).Value = firstVal  'New line[/COLOR]
      Next c
    End If
  End With
    MsgBox "Process Completed Successfully."
    Unload UserForm2
End Sub

Why do you have a UserForm open?
Try closing the UserForm at the beginning of the script
Or explain to me more about what this script is doing.
 
Last edited:
Upvote 0
You might also want to try turning off screen updating and (especially) automatic calculations...

Code:
'at beginning of macro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual



'at end of macro
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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