Consulting

Results 1 to 3 of 3

Thread: Improving Cell Change Macro

  1. #1
    VBAX Regular
    Joined
    Dec 2012
    Posts
    35
    Location

    Improving Cell Change Macro

    Hi Everyone!

    I have some code that does what I want, but if you change the entire 'Target' range at once, it takes a while to cycle through. Any thoughts on how to circumvent the 'For' loop, or optimize in another way?

    Thank you in advance!

    Private Sub worksheet_change(ByVal Target As Range)
    
        For i = 26 To 40
                If Not Intersect(Target, Me.Range("B" & i)) Is Nothing Then
                
                    Worksheets("Primary").Range("$c$" & i & ":$f$" & i).Formula = "=IF(B" & i & "="""","""",VLOOKUP(B" & i & ",Class,2,FALSE))"
            
                End If
        Next
    
    End Sub

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello mikeoly,

    This should run faster.

    Private Sub worksheet_change(ByVal Target As Range)
    
    
        Dim i As Long
        
            Application.EnableEvents = False
            Application.Calculation = xlCalculationManual
        
            If Not Intersect(Target, Range("B26:B40")) Is Nothing Then
                For i = 26 To 40
                    Worksheets("Primary").Cells(i, "C").Resize(1, 4).Formula = "=IF(B" & i & "="""","""",VLOOKUP(B" & i & ",Class,2,FALSE))"
                Next i
            End If
            
            Application.EnableEvents = True
            Application.Calculation = xlCalculationAutomatic
    
    
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    VBAX Regular
    Joined
    Dec 2012
    Posts
    35
    Location
    Thanks, Leith!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •