+ Reply to Thread
Results 1 to 7 of 7

VBA IF Statement & Conditonal Formatting

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA IF Statement & Conditonal Formatting

    Hi, I wonder whether someone may be able to help me please:

    I'm using the code below to apply formulas and formatting to sheet ranges in a sheet array.

    Please Login or Register  to view this content.
    The problem I have is two fold:

    The first, is in relation to this piece of code:

    Please Login or Register  to view this content.
    I'm trying to apply the formula 'RC5-RC6' to column G, but only if the value in column E on the same row is greater than zero. If the value in column E equals zero, I'd like the cell in column G to be blank.

    But unfortunately, when I run this I recieve a Run time '13' error, with debug highlighting this line as the cause:
    Please Login or Register  to view this content.
    The second issue I have is in relation to the result of the above formula shown in column G.

    I'm trying to apply conditional formatting, whereby:

    • If the value is less than zero the font color is red,

    • If thwe value is zero, or greater than zero, change the font color to green.

    I've been working in both of these issue for a few hours now, but I've been unable to find a solution.

    I just wondered whether someone could possibly look at this please and offer soem guidance on how I may go about achieving this.

    Many thanks and kind regards

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: VBA IF Statement & Conditonal Formatting

    Comparing the value of a single cell via the Range(address).Value instruction is supported but you cannot use the same construction for a range of cells in the same manner. If you want to assign the formula for individual cells in column G, you will have two choices:

    1. loop through all cells in the range (E5:E - last row), query the value in cell E and assign the formula or blank value to cell in column G
    2. assign a single formula to all cells in the G column that takes into account the fact that column E might be 0 ("=IF(RC[-2]=0,'',RC5-RC6)")

    as far as formatting red/green is concerned I would suggest to use plain cell formatting (Ctrl-1, Custom Format, for example: [Green]0;[Red]-0)
    If you like my contribution click the star icon!

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA IF Statement & Conditonal Formatting

    Hi @OllieB, thank you once again for taking the time to reply to my post and for the guidance. You clearly have a greater knowledge than I.

    In respect of the formula, I think I'm right in saying that option 2 would be be more efficient? If so, could you perhaps elaborate on how I may incorporate this please?

    In respect of the conditional formatting, I understand what you're saying, but I'm trying to take away as much manual intervention as possible, because the macro I'm building may be used by colleagues, who have very little knowledge of Excel.

    Many thanks and kind regards

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: VBA IF Statement & Conditonal Formatting

    With regards to the first point

    Replace
    Please Login or Register  to view this content.
    By
    Please Login or Register  to view this content.
    with regards to the second part, just select the column where the values are show, press {Ctrl}{1}, Select category "custom" and copy & paste "[Green]0;[Red]-0" into the textbox under Type:. click OK button - that's it

    nothing manual about it as it needs to be done only once.

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA IF Statement & Conditonal Formatting

    Hi @OllieB thank you very much for this on both counts.

    I've amended the code you suggested, but unfortunately I receive the following error message: Run time error '1004': Application-defined or object-defined.

    Debug highlights the line I have just introduced as the cause?

    Many thanks and kind regards

  6. #6
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: VBA IF Statement & Conditonal Formatting

    Sorry, I though single quotes would do it.

    Replace by formula line by

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA IF Statement & Conditonal Formatting

    Hi @OllieB, thank you very much for coming back to me with this.

    I has to tweak the 'RC' references a little, but it works great.

    All the best and kind regards

+ 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] Conditonal formatting if/then
    By adamjohnson182 in forum Excel General
    Replies: 3
    Last Post: 05-29-2012, 07:15 PM
  2. Conditonal Formatting
    By rideanon in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 06-08-2010, 12:40 PM
  3. Conditonal Formatting
    By Lorna B in forum Excel General
    Replies: 4
    Last Post: 09-15-2009, 08:16 AM
  4. VBA Conditonal Formatting
    By Salient in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2009, 01:34 PM
  5. Conditonal Formatting
    By Matt in forum Excel General
    Replies: 3
    Last Post: 04-21-2006, 02:45 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