+ Reply to Thread
Results 1 to 6 of 6

Add border & apply conditional formatting to sheets based on cell value

Hybrid View

amar05 Add border & apply... 08-13-2013, 06:40 PM
jaslake Re: Add border & apply... 08-13-2013, 07:35 PM
amar05 Re: Add border & apply... 08-14-2013, 09:24 AM
jaslake Re: Add border & apply... 08-14-2013, 10:11 AM
amar05 Re: Add border & apply... 08-14-2013, 10:36 AM
jaslake Re: Add border & apply... 08-14-2013, 11:02 AM
  1. #1
    Registered User
    Join Date
    08-30-2012
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    59

    Add border & apply conditional formatting to sheets based on cell value

    Hi all,
    I have a macro that applies borders and conditional formatting to a sheet based on a cell value. The Workbook has 20+ worksheets and the sheet names may change. I am looking for a macro that will apply the below code it to all worksheets except for 'Sheet1', 'Sheet2' and 'Sheet3'.

        Sheets("Sample").Select
        If Range("A3").Value > "0" Then Call add
        .
        .
    Sub add()
    
    'add border
        Cells.Select
        Selection.Borders.LineStyle = xlNone
        Range("A2").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        With Selection.Borders
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
        End With
        
    'apply conditional formatting
    
        Range("A3:U3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=MOD(ROW(),2)=0"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorLight2
            .TintAndShade = 0.799981688894314
        End With
        Selection.FormatConditions(1).StopIfTrue = False
    
    End Sub
    I believe that the code I am looking for, has to start w/
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    but I don't know how to adjust the above code.

    Any help is much appreciated.

    Thanks

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Add border & apply conditional formatting to sheets based on cell value

    Hi amar05

    Probably something like this
    Option Explicit
    Sub add()
        Dim ws As Worksheet
    
        For Each ws In ActiveWorkbook.Worksheets
            If Not ws.Name = "Sheet1" And Not ws.Name = "Sheet2" _
                    And Not ws.Name = "Sheet3" Then
    
                'add border
                Cells.Select
                Selection.Borders.LineStyle = xlNone
                Range("A2").Select
                Range(Selection, Selection.End(xlToRight)).Select
                Range(Selection, Selection.End(xlDown)).Select
                With Selection.Borders
                    .LineStyle = xlContinuous
                    .Weight = xlThin
                    .ColorIndex = xlAutomatic
                End With
    
                'apply conditional formatting
    
                Range("A3:U3").Select
                Range(Selection, Selection.End(xlDown)).Select
                Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
                        "=MOD(ROW(),2)=0"
                Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
                With Selection.FormatConditions(1).Interior
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorLight2
                    .TintAndShade = 0.799981688894314
                End With
                Selection.FormatConditions(1).StopIfTrue = False
            End If
        Next ws
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    08-30-2012
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    59

    Re: Add border & apply conditional formatting to sheets based on cell value

    Hi John,
    The code worked, but how can I integrate this part:

        Sheets("Sample").Select
        If Range("A3").Value > "0" Then Call add
    It has to check cell A3 in all the sheets (except for sheet1, sheet2 and sheet3) for any values before adding the rows & cond. formatting.

    Thank you.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Add border & apply conditional formatting to sheets based on cell value

    Hi amar05

    Your doing a lot of selecting which probably isn't necessary but try this (untested)
    Option Explicit
    Sub add()
        Dim ws As Worksheet
    
        For Each ws In ActiveWorkbook.Worksheets
            If Not ws.Name = "Sheet1" And Not ws.Name = "Sheet2" _
                    And Not ws.Name = "Sheet3" Then
                With ws
                    .Activate
                    If .Range("A3").Value > 0 Then
    
                        'add border
                        .Cells.Select
                        Selection.Borders.LineStyle = xlNone
                        .Range("A2").Select
                        .Range(Selection, Selection.End(xlToRight)).Select
                        .Range(Selection, Selection.End(xlDown)).Select
                        With Selection.Borders
                            .LineStyle = xlContinuous
                            .Weight = xlThin
                            .ColorIndex = xlAutomatic
                        End With
    
                        'apply conditional formatting
                        .Range("A3:U3").Select
                        .Range(Selection, Selection.End(xlDown)).Select
                        Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
                                "=MOD(ROW(),2)=0"
                        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
                        With Selection.FormatConditions(1).Interior
                            .PatternColorIndex = xlAutomatic
                            .ThemeColor = xlThemeColorLight2
                            .TintAndShade = 0.799981688894314
                        End With
                        Selection.FormatConditions(1).StopIfTrue = False
                    End If
                End With
            End If
        Next ws
    End Sub

  5. #5
    Registered User
    Join Date
    08-30-2012
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    59

    Re: Add border & apply conditional formatting to sheets based on cell value

    Worked perfectly. That's what I was looking for.

    Thanks so much for your help.

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Add border & apply conditional formatting to sheets based on cell value

    You're welcome...glad I could help. Thanks for the Rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA for Conditional Formatting - apply a colour based on conditions in each column.
    By acsishere in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-27-2013, 01:24 PM
  2. Replies: 3
    Last Post: 04-30-2011, 11:46 PM
  3. Apply Border based on change in cell value?
    By efeb in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-11-2009, 04:41 PM
  4. [SOLVED] HOW CAN I APPLY CONDITIONAL FORMATTING BASED TO AN ENTIRE ROW?
    By James M in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-19-2005, 11:45 AM
  5. [SOLVED] Apply Border based on last Day of Month
    By Bill in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-22-2005, 03:05 PM

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