+ Reply to Thread
Results 1 to 11 of 11

Need Macro to Format Cells based on corresponding cell values

Hybrid View

  1. #1
    Registered User
    Join Date
    03-07-2012
    Location
    peace cjourts
    MS-Off Ver
    Excel 2003
    Posts
    10

    Need Macro to Format Cells based on corresponding cell values

    Hi,

    I have a pretty large sheet (3000+ rows), whereby if the cell in column A of a row contains a number of between 1 and 150, then I would like the corresponding cell in Column E to be grey with a border around it. Obviously an arduous task if done using Copy & Paste and unfortunately I need this to happen in a number of sheets and don't fancy RSI at the end of the exercise.

    As part of a second part of the project, I need to be able to 'Unlock' the cells that end up as grey w/border so that I can protect the sheet and only leave the grey cells as editable.

    Hope someone can help!

    TJ

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Need Macro to Format Cells based on corresponding cell values

    For the first part maybe:

    Sub feckoffcup()
    Dim lr As Long
    Dim rcell As Range
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    
    For Each rcell In Range("A2:A" & lr)
    
        If rcell.Value > 1 Then
        
            If rcell.Value < 150 Then
            
                rcell.Offset(, 4).Select
                
                        With Selection.Borders(xlEdgeLeft)
                            .LineStyle = xlContinuous
                            .Weight = xlMedium
                            .ColorIndex = xlAutomatic
                        End With
                        With Selection.Borders(xlEdgeTop)
                            .LineStyle = xlContinuous
                            .Weight = xlMedium
                            .ColorIndex = xlAutomatic
                        End With
                        With Selection.Borders(xlEdgeBottom)
                            .LineStyle = xlContinuous
                            .Weight = xlMedium
                            .ColorIndex = xlAutomatic
                        End With
                        With Selection.Borders(xlEdgeRight)
                            .LineStyle = xlContinuous
                            .Weight = xlMedium
                            .ColorIndex = xlAutomatic
                        End With
                    
                rcell.Offset(, 4).Interior.ColorIndex = 15
                
            End If
            
        End If
        
    Next rcell
                
    End Sub

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Need Macro to Format Cells based on corresponding cell values

    Using the code John alread started...

    Sub feckoffcup()
        Dim lr As Long
        Dim rcell As Range
        Dim ws As Worksheet
        Set ws = Sheets("Sheet4")    
        ws.Unprotect "password"
        Application.ScreenUpdating = False
        lr = Cells(Rows.Count, 1).End(xlUp).Row
        For Each rcell In Range("A2:A" & lr)
            If rcell.Value > 1 And rcell.Value < 150 Then
                 With rcell.Offset(, 4)
                    With .Borders
                        .LineStyle = xlContinuous
                        .Weight = xlMedium
                        .ColorIndex = xlAutomatic
                    End With
                    .Interior.ColorIndex = 15
                    .Locked = False
                End With
            End If
        Next rcell
        ws.Protect "password"
        Application.ScreenUpdating = True
    End Sub
    Last edited by jeffreybrown; 11-14-2012 at 04:20 PM.
    HTH
    Regards, Jeff

  4. #4
    Registered User
    Join Date
    03-07-2012
    Location
    peace cjourts
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Need Macro to Format Cells based on corresponding cell values

    Thanks amillion guys for looking at this, I am getting a syntax error on the following line:

        Set ws = Sheets("Sheet4")
    I have changed "Sheet4" (which it just happened to be coincidentally!) to the name of my sheet "Business" but I am getting the same error.

  5. #5
    Registered User
    Join Date
    03-07-2012
    Location
    peace cjourts
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Need Macro to Format Cells based on corresponding cell values

    Sorry - just realised that the color kicked in within the CODE, the line did include the COLOR as you had included!

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Need Macro to Format Cells based on corresponding cell values

    Are you sure the spelling is correct because the macro worked for me?

  7. #7
    Registered User
    Join Date
    03-07-2012
    Location
    peace cjourts
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Need Macro to Format Cells based on corresponding cell values

    I changed the name of the Worksheet to Sheet4 just in case, however I am still getting the same message - screenshot attached:

    Attachment 193732

    As you can see I attempted to change Sheets to Worksheets also to no avail - the error message is identical when Sheets is in place

  8. #8
    Registered User
    Join Date
    03-07-2012
    Location
    peace cjourts
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Need Macro to Format Cells based on corresponding cell values

    I changed the name of the Worksheet to Sheet4 just in case, however I am still getting the same message - screenshot attached:

    Capture.JPG

  9. #9
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Need Macro to Format Cells based on corresponding cell values

    Remove all that color stuff, it should just read...

    Set ws = Sheets("Sheet4")
    Sorry about that I fixed post #3

  10. #10
    Registered User
    Join Date
    03-07-2012
    Location
    peace cjourts
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Need Macro to Format Cells based on corresponding cell values

    Thats at Jeff - worked a treat - thank you very much!

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Need Macro to Format Cells based on corresponding cell values

    You are very welcome and glad you now have a working model. Thanks for the feedback.

    ============================================================================

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

+ 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