+ Reply to Thread
Results 1 to 7 of 7

VBA Code for Conditional Formatting Not Working

  1. #1
    Registered User
    Join Date
    02-08-2014
    Location
    Perth
    MS-Off Ver
    Excel 2003
    Posts
    26

    VBA Code for Conditional Formatting Not Working

    Hi All,

    I was trying to revise a code by adding bits for conditional formatting - if duration is less then a certain number text is aligned left, else its aligned right. The formatting is done within text boxes - shapes. Please see bits of the code below. Is there anything wrong there? Its not working properly as it always aligns the text to the right, never to the left. Any thoughts?

    duration = Round((EndTime - StartTime) * 24 * 60)
    Set GantShape = Worksheets(destSheet).Shapes.AddShape(msoShapeRectangle, LeftPoints, TopPoints, WidthPoints, HeightPoints)

    With GantShape
    .Fill.ForeColor.RGB = PatternType.Color

    .Line.Weight = 0.25
    .Name = "WorkingShape"
    If inBoxString <> "" Then

    Select Case equipment
    Case "A"
    If duration < 1.5 Then
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlLeft
    Else
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlRight

    End If

    Case "B"
    If duration < 0.5 Then
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlLeft
    Else
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlRight

    End If

    Case "C"
    If duration < 5 Then
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlLeft
    Else
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlRight

    End If

    Case "D"
    If duration < 60 Then
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlLeft
    Else
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlRight

    End If

    Case "E"
    If duration * 24 < 0.1 Then
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlLeft
    Else
    .TextFrame.Characters.Text = inBoxString
    .TextFrame.Characters.Font.Size = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.FontStyle = PatternType.Parent.Font.Size
    .TextFrame.Characters.Font.Color = PatternType.Parent.Font.Color
    .TextFrame.VerticalAlignment = xlTop
    .TextFrame.HorizontalAlignment = xlRight

    End If
    End Select

    End If
    End With

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA Code for Conditional Formatting Not Working

    Hi, and welcome to the forum.
    Unfortunately your post does not comply with Rule 3 of our Forum
    RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    02-08-2014
    Location
    Perth
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: VBA Code for Conditional Formatting Not Working

    Sorry for that, hope this below is better now. Any help would be much appreciated.

    Please Login or Register  to view this content.
    Quote Originally Posted by Richard Buttrey View Post
    Hi, and welcome to the forum.
    Unfortunately your post does not comply with Rule 3 of our Forum
    RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

  4. #4
    Registered User
    Join Date
    02-08-2014
    Location
    Perth
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: VBA Code for Conditional Formatting Not Working

    Please Login or Register  to view this content.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA Code for Conditional Formatting Not Working

    Hi,

    Which equipment ref is giving you the wrong result?

    Are you sure you want to multiply the time difference by 24 x 60. In the case of equipment A unless the time is difference is less than 90 seconds you'll never get left aligned text.

  6. #6
    Registered User
    Join Date
    02-08-2014
    Location
    Perth
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: VBA Code for Conditional Formatting Not Working

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Which equipment ref is giving you the wrong result?

    Are you sure you want to multiply the time difference by 24 x 60. In the case of equipment A unless the time is difference is less than 90 seconds you'll never get left aligned text.
    Hi, thank for the reply.

    That's the problem, every piece of equipment is giving me a wrong result.

    I actually want the text to be bold and red, not aligned right, this was just a test.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA Code for Conditional Formatting Not Working

    Hi,

    I think you'd better upload the ACTUAL workbook and tell us what result you expect given specific inputs/selections.

+ 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. [SOLVED] Conditional formatting not working
    By boomboomblock in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-22-2013, 06:27 AM
  2. [SOLVED] Conditional Formatting using VBA - Code not working properly
    By Tejas.T in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-07-2012, 10:19 AM
  3. Conditional Formatting not Working
    By braydon16 in forum Excel General
    Replies: 7
    Last Post: 05-23-2012, 02:41 PM
  4. Conditional Formatting code not working
    By a8747 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-25-2011, 02:22 PM
  5. Conditional Formatting not working
    By Crowbar via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2005, 03:15 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