Need code to run faster

User Name Active

New Member
Joined
Jan 29, 2014
Messages
19
This code works great and does what I need it to, but it takes to long. I need users to run this code constantly and I need it to run faster.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

 
    Set KeyCells = Range("D4:D8,D11")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then
  
Range("D14") = "=J41"
Range("D15") = "=K41"
Range("D16") = "=L41"
Range("D19") = "=M41"
Range("D20") = "=N41"
Range("D21") = "=O41"
Range("H14") = "=P41"
Range("H15") = "=Q41"
Range("H16") = "=R41"
Range("H17") = "=S41"
Range("H18") = "=T41"
Range("H19") = "=U41"
Range("H20") = "=V41"
Range("H21") = "=W41"
Range("H22") = "=X41"
Range("H23") = "=Y41"
Range("H24") = "=Z41"
Range("H25") = "=AA41"
Range("H26") = "=AB41"
Range("H27") = "=AC41"
Range("H28") = "=AD41"
Range("H29") = "=AE41"
Range("H30") = "=AF41"
Range("H31") = "=AG41"
Range("H32") = "=AH41"
Range("H33") = "=AI41"
Range("H34") = "=AJ41"


Set KeyCells = Range("D7:D7")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

Range("D9") = "=C58"
Range("D10") = "=C59"
    End If
    
End If
    
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
That code will call itself quite a few times.

Try turning off events to prevent that.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range

    Set KeyCells = Range("D4:D8,D11")

    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
        Application.EnableEvents = False

        Range("D14") = "=J41"
        Range("D15") = "=K41"
        Range("D16") = "=L41"
        Range("D19") = "=M41"
        Range("D20") = "=N41"
        Range("D21") = "=O41"
        Range("H14") = "=P41"
        Range("H15") = "=Q41"
        Range("H16") = "=R41"
        Range("H17") = "=S41"
        Range("H18") = "=T41"
        Range("H19") = "=U41"
        Range("H20") = "=V41"
        Range("H21") = "=W41"
        Range("H22") = "=X41"
        Range("H23") = "=Y41"
        Range("H24") = "=Z41"
        Range("H25") = "=AA41"
        Range("H26") = "=AB41"
        Range("H27") = "=AC41"
        Range("H28") = "=AD41"
        Range("H29") = "=AE41"
        Range("H30") = "=AF41"
        Range("H31") = "=AG41"
        Range("H32") = "=AH41"
        Range("H33") = "=AI41"
        Range("H34") = "=AJ41"

        Set KeyCells = Range("D7")

        If Not Application.Intersect(KeyCells, Target) Is Nothing Then

            Range("D9") = "=C58"
            Range("D10") = "=C59"
        End If

    End If

    Application.EnableEvents = True

End Sub
 
Upvote 0
Perhaps also clean it up.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

  Dim i As Integer
  
  Application.EnableEvents = False
  
  If Not Intersect(Me.Range("D4:D8,D11"), Target) Is Nothing Then
    For i = 10 To 36
      Select Case i
        Case Is < 13
          Me.Cells(i + 4, "D").Formula = "=" & Me.Cells(41, i).Address(False, False)
        Case Is < 16
          Me.Cells(i + 6, "D").Formula = "=" & Me.Cells(41, i).Address(False, False)
        Case Else
          Me.Cells(i - 2, "H").Formula = "=" & Me.Cells(41, i).Address(False, False)
      End Select
    Next i
  End If
  
  If Not Intersect(Me.Range("D7"), Target) Is Nothing Then
    Me.Range("D9:D10").Formula = "=C58"
  End If

  Application.EnableEvents = True
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,850
Members
449,194
Latest member
HellScout

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