+ Reply to Thread
Results 1 to 2 of 2

Simplifying code

Hybrid View

  1. #1
    Registered User
    Join Date
    02-20-2012
    Location
    Baltimore, MD
    MS-Off Ver
    Excel 2010
    Posts
    30

    Simplifying code

    I am writing some code that would change a few rows of cells if a checkbox is ticked, any idea on how to simplify the code I got? Any help and suggestions would be appreciated! Thank you!


    Private Sub BoxTick_Click()
    If BoxTick.Value = True Then
    Sheets("Sheet2").Unprotect
    Sheets("Sheet2").Range("C26").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("D26").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("C24").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("D24").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("C25").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("D25").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("C30").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("D30").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("D28").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("C28").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("C29").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Range("D29").FormulaR1C1 = "N/A"
    Sheets("Sheet2").Rows("22:22").EntireRow.Hidden = True
    Sheets("Sheet2").Rows("24:26").EntireRow.Hidden = True
    Sheets("Sheet2").Rows("28:30").EntireRow.Hidden = True
    Sheets("Sheet2").Range("A23").FormulaR1C1 = "12"
    Sheets("Sheet2").Range("A31").FormulaR1C1 = "13"
    Sheets("Sheet2").Range("A32").FormulaR1C1 = "14"
    Sheets("Sheet2").Range("A34").FormulaR1C1 = "15"
    Sheets("Sheet2").Range("A35").FormulaR1C1 = "16"
    Sheets("Sheet2").Range("A36").FormulaR1C1 = "17"
    Sheets("Sheet2").Range("A37").FormulaR1C1 = "18"
    Sheets("Sheet2").Range("A38").FormulaR1C1 = "19"
    Sheets("Sheet2").Range("A40").FormulaR1C1 = "20"
    Sheets("Sheet2").Range("A41").FormulaR1C1 = "21"
    Sheets("Sheet2").Range("A42").FormulaR1C1 = "22"
    Sheets("Sheet2").Range("A43").FormulaR1C1 = "23"
    Sheets("Sheet2").Range("A44").FormulaR1C1 = "24"
    Sheets("Sheet2").Range("A45").FormulaR1C1 = "25"
    Sheets("Sheet2").Range("A46").FormulaR1C1 = "26"
    Sheets("Sheet2").Range("A47").FormulaR1C1 = "27"
    Sheets("Sheet2").Range("A48").FormulaR1C1 = "28"
    Sheets("Sheet2").Protect
    End If
    End Sub
    Thank you tigeravatar! Here is the simplifed code from tigeravatar:

    Quote Originally Posted by tigeravatar View Post
    Private Sub BoxTick_Click()
        
        If BoxTick.Value = True Then
            
            Dim i As Long
            Dim iCell As Range
            
            With Sheets("Sheet2")
                .Unprotect
                .Range("C24:D26,C28:D29").Value = "N/A"
                .Range("22:22,24:26,28:30").EntireRow.Hidden = True
                
                i = 12
                For Each iCell In .Range("A23,A31:A32,A34:A38,A40:A48")
                    iCell.Value = i
                    i = i + 1
                Next iCell
                
                .Protect
            End With
            
        End If
        
    End Sub
    Last edited by cg0789; 03-16-2012 at 08:43 AM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Simplying code

    cg0789,

    One way...
    Private Sub BoxTick_Click()
        
        If BoxTick.Value = True Then
            
            Dim i As Long
            Dim iCell As Range
            
            With Sheets("Sheet2")
                .Unprotect
                .Range("C24:D26,C28:D29").Value = "N/A"
                .Range("22:22,24:26,28:30").EntireRow.Hidden = True
                
                i = 12
                For Each iCell In .Range("A23,A31:A32,A34:A38,A40:A48")
                    iCell.Value = i
                    i = i + 1
                Next iCell
                
                .Protect
            End With
            
        End If
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

+ 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