+ Reply to Thread
Results 1 to 3 of 3

Apply Conditonal Formatting based on a relative cell address in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    03-23-2015
    Location
    UK
    MS-Off Ver
    2016
    Posts
    27

    Apply Conditonal Formatting based on a relative cell address in VBA

    I'm trying to add a couple of FormatConditions to a range based on Formulas (xlExpression?).

    The range spans a couple of table columns so I need to build the formula based on a relative cell address so the formatting is referencing the cell's own value.

    I have this working fine via Excel's Conditional Formatting, but I'm struggling with the VBA code.

    Building the range was fine:
                    'get the range
                    rangeAdd = Range(tb & "[" & currentDay.ColumnName(formattingItem) & "]")
                    
                    
                    'remove existing formatting
                    rangeAdd.FormatConditions.Delete
                    
                    
                    'add to the new range
                    If Not rangeFormatting Is Nothing Then
                        Set rangeFormatting = Union(rangeFormatting, rangeAdd)
                    Else
                        Set rangeFormatting = rangeAdd
                    End If
    The conditional formatting in Excel, looks like this:
    =(AQ6="TOP")
    How do I convert my first table cell reference so it reads "AQ6", and how do I build the formatting condition in VBA?

        Dim condition_Top As FormatCondition
        
        With rng
            Set condition_Top = .FormatConditions.Add(xlExpression, "=" & sAddress & "=""TOP""")
        End With
    End Sub
    
    Public Sub SetFormattingCondition(condition As FormatCondition, cellColor)
    
        With condition
            .Interior.Color = cellColor
        End With
    
    End Sub

    Any help would be greatly appreciated.

    Thanks.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Apply Conditonal Formatting based on a relative cell address in VBA

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    03-23-2015
    Location
    UK
    MS-Off Ver
    2016
    Posts
    27

    Re: Apply Conditonal Formatting based on a relative cell address in VBA

    Got everything working!

    Public Sub SetFormattingCondition(rng As Range, expression As String, cellColor)
        Dim condition As FormatCondition
        
        With rng
            Set condition = .FormatConditions.Add(xlExpression, , "=" & expression)
        End With
    
        With condition
            .StopIfTrue = True
            .Interior.Color = cellColor
        End With
        
    End Sub
    In future, I will not cross post. Wasn't aware this was an issue!

+ 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. Replies: 1
    Last Post: 10-04-2016, 05:17 AM
  2. [SOLVED] Loop through workbook and apply conditonal formatting based on previous sheet
    By beenbee in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-08-2016, 06:30 AM
  3. Apply Conditional Formatting to Relative Range
    By wotaj in forum Excel General
    Replies: 1
    Last Post: 09-18-2014, 12:44 PM
  4. Replies: 2
    Last Post: 06-05-2013, 11:03 AM
  5. Conditonal formatting based on adjacent cells
    By archiebongo in forum Excel General
    Replies: 4
    Last Post: 04-27-2010, 02:27 AM
  6. Conditonal Formatting a Cell Based on Other Cells
    By fasterthanyours in forum Excel General
    Replies: 14
    Last Post: 08-04-2009, 12:24 PM
  7. Conditonal formatting based off date&time
    By crizzle in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-01-2008, 03:06 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