+ Reply to Thread
Results 1 to 3 of 3

Slow (looping) macro - Need help adjusting it

Hybrid View

  1. #1
    Forum Contributor PY_'s Avatar
    Join Date
    09-23-2008
    Location
    Houston
    MS-Off Ver
    Office 2016
    Posts
    289

    Slow (looping) macro - Need help adjusting it

    I know this macro is not the most efficient macro just because of the loops and how long it takes my machine to run. Is there another way to write this so it doesnt make my 5yr+ old machine just come to a halt while running it?


    ' Lock Open Walls Cells - Partition Walls - Framed Openings - Below Eave Canopies
        Private Sub lockCells_OpenWalls()
                Dim i As Long
                Dim j As Long
                Dim k As Long
        ' Lock Framed Opening Cells
            Sheets("Gen_3").Activate
            ActiveSheet.Unprotect Password:="MyPassword"
            For i = 278 To 341
                If Range("G" & i).Value = "" Or _
                    Range("G" & i).Value = "n/a" Then
                        Range("O" & i & ":V" & i).Locked = True
                        Range("X" & i & ":AF" & i).Locked = True
                        End If
                If InStr(Range("G" & i).Value, "Open") Then
                    Range("O" & i & ":V" & i).Locked = False
                    Range("X" & i & ":AF" & i).Locked = True
                    End If
                If InStr(Range("C" & i).Value, "Front") Or _
                    InStr(Range("C" & i).Value, "Back") Then
                        Range("X" & i & ":AF" & i).Locked = True
                        End If
                If Range("x" & i).Value = "Select Endwall Type" Then
                    Range("X" & i & ":AF" & i).Locked = False
                    End If
                Next i
        ' Lock Framed Opening Cells
            For j = 494 To 597
                If Range("G" & j).Value = "" Then
                    Range("N" & j & ":AH" & j).Locked = True
                    End If
                Next j
        ' Lock Below Eave Canopies
            For k = 346 To 473
                If Range("G" & k).Value = "" Or _
                    Range("G" & k).Value = "n/a" Or _
                    InStr(Range("C" & k).Value, "Soffit") Then
                        Range("Q" & k & ":AH" & k).Locked = True
                        End If
                    Range("U" & k).Locked = True
                Next k
    End Sub

  2. #2
    Forum Contributor
    Join Date
    06-09-2011
    Location
    Germany
    MS-Off Ver
    Excel 2016
    Posts
    194

    Re: Slow (looping) macro - Need help adjusting it

    Hi,

    maybe you should add following commands after the Variable Declarations:

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Before the End Sub, you will need to set them back to normal:

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Regards

    Theo
    __________________
    Please consider:

    Thanking those who helped you. Click the scales icon in the upper right corner of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Slow (looping) macro - Need help adjusting it

    Hi

    You refer to the same cell multiple times - it is quicker to put the cell value into memory as a variable

    The way you i loop is coded you have the potential to lock and unlock the same cells multiple times

    this is 1 way to overcome that problem - I have only looked at fixing your i loop

    Note:- with the instr command using "Front|Back" I have never used the | to join to items but got the info from
    Dim i As Long
    Dim j As Long
    Dim k As Long

    Dim bOV As Boolean
    Dim bXAF As Boolean
    Dim sTxt As String

    ' Lock Framed Opening Cells
    Sheets("Gen_3").Activate
    ActiveSheet.Unprotect Password:="MyPassword"


    For i = 278 To 341
    sTxt = Range("G" & i).Value
    Select Case sTxt
    Case "", "n/a"
    bOV = True
    bXAF = True
    End Select
    If InStr(sTxt, "Open") Then
    bOV = False
    bXAF = True
    End If

    sTxt = Range("C" & i).Value
    '*****************************************************************
    'you could change this use Or instead of an ElseIf
    'I do not know the speed difference between the 2 methods
    '*******************************************************************
    If InStr(sTxt, "Front") Then
    bXAF = True
    ElseIf nStr(sTxt, "Back") Then
    bXAF = True
    End If

    If Range("x" & i).Value = "Select Endwall Type" Then
    bXAF = False
    End If

    Range("O" & i & ":V" & i).Locked = bOV
    Range("X" & i & ":AF" & i).Locked = bXAF
    Next i
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1