+ Reply to Thread
Results 1 to 5 of 5

Perform macro based on cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2009
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    66

    Perform macro based on cell value

    Hi all, I have a macro in place but it is very sluggish.

    When i exit a specific sheet (Sheet 3), I would like the values in columns K,L and M to be fixed if the corrosponding row value in column H is "Y".

    I have the below code but as mentioned it is very time consuming and freezes the screen when running. if there is a better way please could someone assist?



    Private Sub Worksheet_Deactivate()
    Application.ScreenUpdating = False
    
     Sheet3.Unprotect 
    Dim lRow As Long, lCounter As Long
     lRow = Cells(Rows.Count, 1).End(xlUp).Row
     For lCounter = 2 To lRow
        If Cells(lCounter, 8) = "Y" Then
            With Cells(lCounter, 11).Resize(1, 3)
            .Value = .Value
                End With
            
        End If
     Next lCounter
     Sheet3.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True, 
     Application.ScreenUpdating = True
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Perform macro based on cell value

    Try this:

    Private Sub Worksheet_Deactivate()
    Application.ScreenUpdating = False
    
    Dim iRow as Range
    Dim FirstMatch
    
     Sheet3.Unprotect 
    
      Set iRow=Column(8).Find("Y", Lookat:=xlWhole)
      If Not iRow is Nothing Then
         FirstMatch=iRow.Address
         While Not iRow Is Nothing
            iRow.Resize(1, 3)
           Set iRow=Column(8).FindNext(iRow)
           If iRow.Address=FirstMatch Then Set iRow=Nothing
         Wend
       End If
    
     Sheet3.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
            , AllowFiltering:=True, 
     Application.ScreenUpdating = True
    End Sub

  3. #3
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Perform macro based on cell value

    You could try turning calculation off while the macro works:

    Private Sub Worksheet_Deactivate()
    
    Dim myCalc
    Dim lRow As Long, lCounter As Long
    
    myCalc = Application.Calculation
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    
    Sheet3.Unprotect
    
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For lCounter = 2 To lRow
        If Cells(lCounter, 8) = "Y" Then
            With Cells(lCounter, 11).Resize(1, 3)
                .Value = .Value
            End With
        End If
    Next lCounter
    
    Sheet3.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFiltering:=True
    
    Application.ScreenUpdating = True
    Application.Calculation = myCalc
    
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  4. #4
    Registered User
    Join Date
    04-21-2009
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    66

    Re: Perform macro based on cell value

    Hi, I get an error on the iRow.Resize(1, 3) saying an = is required

  5. #5
    Registered User
    Join Date
    04-21-2009
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    66

    Re: Perform macro based on cell value

    Hi Domski - that worked perfectly. Thank you

+ 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