+ Reply to Thread
Results 1 to 3 of 3

Conditional Formats .activate not working in Personal.xls

Hybrid View

  1. #1
    Registered User
    Join Date
    05-31-2013
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    17

    Conditional Formats .activate not working in Personal.xls

    I have created a code which runs perfectly in the standard module of the file but I want this code to be available for use in other books. When I place this code in my Personal.xls, it creates an error on the .Activate of the conditional formats. How would I go about fixing this? Also, I there any way that I can reduce the code for easier reading since the formatting is the same just different formulas to adhere to.

    Here is the snippet of code I am referencing:

    'Adds conditional formats to columns in red font for errors to check
    'LastRow = Range("L" & Rows.Count).End(xlUp).Row
    
    With Sheet1
            With .Range("D2:D" & LastRow)
                .Activate
                .FormatConditions.Add xlExpression, Formula1:="=LEN(D2)>=255"
                .FormatConditions(1).Interior.ColorIndex = 3
                .FormatConditions(1).Font.ColorIndex = 3
            End With
            With .Range("F2:F" & LastRow)
                .Activate
                .FormatConditions.Add xlExpression, Formula1:="=LEN(F2)>=255"
                .FormatConditions(1).Interior.ColorIndex = 3
                .FormatConditions(1).Font.ColorIndex = 3
            End With
            With .Range("H2:H" & LastRow)
                .Activate
                .FormatConditions.Add xlExpression, Formula1:="=LEN(H2)>=90"
                .FormatConditions(1).Interior.ColorIndex = 3
                .FormatConditions(1).Font.ColorIndex = 3
            End With
            With .Range("J2:J" & LastRow)
                .Activate
                .FormatConditions.Add xlExpression, Formula1:="=LEN(J2)>=700"
                .FormatConditions(1).Interior.ColorIndex = 3
                .FormatConditions(1).Font.ColorIndex = 3
            End With
        End With
    Thank you once again!

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Conditional Formats .activate not working in Personal.xls

    Hi,

    This:
    With Sheet1
    is the worksheet with Codename Sheet1 of Personal.xls, no other sheet and you cannot use codenames to refer to a sheet in a different workbook then the code is.
    If you want to use it on the sheet you have activated use
    With ActiveSheet
    a shorted code:
    Sub test()
        Dim aConditions: aConditions = Array(255, 255, 90, 700)
        Dim LastRow As Long: LastRow = 50   'testing
        Dim i As Integer, j As Integer
    '
        With ActiveSheet
            j = 0
            For i = 4 To 10 Step 2
                With .Range(.Cells(2, i), .Cells(LastRow, i))
                    .FormatConditions.Add xlExpression, Formula1:="=LEN(" & .Cells(1, 1).Address(False, False) & ")>=" & aConditions(j)
                    .FormatConditions(1).Interior.ColorIndex = 3
                    .FormatConditions(1).Font.ColorIndex = 3
                End With
                j = j + 1
            Next i
        End With
    End Sub
    Last edited by tehneXus; 06-25-2013 at 03:57 PM. Reason: added shorter code
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    05-31-2013
    Location
    Orlando, FL
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Conditional Formats .activate not working in Personal.xls

    Thank you, I have been working on this all day and thought it was something just that simple. Time for a break!

+ 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