+ Reply to Thread
Results 1 to 3 of 3

Combine code in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    02-18-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2010
    Posts
    17

    Combine code in VBA

    Here's my code, how can I simplify it? I'm checking the values in a range of 4 cells against a single cell, then unprotecting two adjacent cells on each row if the value is true.

        Sheets("Quarterly Bonus").Select
        For Each cell In Range("B15")
        If Range("B15") = Range("f10") Then
        Range("D15:E15").Select
        Selection.Locked = False
        End If
        Next
        
        For Each cell In Range("B16")
        If Range("B16") = Range("f10") Then
        Range("D16:E16").Select
        Selection.Locked = False
        End If
        Next
        
        For Each cell In Range("B17")
        If Range("B17") = Range("f10") Then
        Range("D17:E17").Select
        Selection.Locked = False
        End If
        Next
    
        For Each cell In Range("B18")
        If Range("B18") = Range("f10") Then
        Range("D18:E18").Select
        Selection.Locked = False
        End If
        Next
    Last edited by clifton1230; 03-16-2013 at 12:20 AM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Combine code in VBA

    Try this.
    With Sheets("Quarterly Bonus")
        For Each cell In .Range("B15:B18")
          If cell.Value = Range("F10").Value Then
             cell.Offset(,2).Resize(,2).Locked =False
          End If
        Next cell
    End With
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-18-2013
    Location
    Dallas
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Combine code in VBA

    Perfect, changed "Offset" to one because the first cell was in the next column from my test cell.

    Brilliant - thanks!

+ 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