+ Reply to Thread
Results 1 to 13 of 13

Conditional formatting from separate pages

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Conditional formatting from separate pages

    Is there a formula I can enter into the conditional formula palette that will allow me to format cells on one sheet based on a match found on a second sheet? ....or do I have to use VBA to do this??

    Here, specifically, is what I am trying to do:

    Sheet1 A1:AA200 contains a list of names.
    Sheet2 A1:A200 consists of a Range named MALE.
    Sheet2 B1:B200 consists of a Range named FEMALE.

    Consider, first...that some of the cells in BOTH named ranges may be blank.

    I want to format all of the cells in Sheet1 A1:AA200 to turn green if a match is found in the Range named MALE....and turn the cells red if a match is found in the Range named FEMALE.

    When I try to use the Range names in my formula...I get various errors.

    Please advise me if I need to use VBA to accomplish this. Writing VBA to do this should be relatively simple...but I would appreciate some sample code to use as a model, since my VBA coding skills are still in their infancy.

    Thanks, in advance, for any help you can give me.
    Last edited by VBA Noob; 01-09-2008 at 06:58 PM.

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,694
    There's no reason why you can't use named ranges in your conditional formatting formulas, in fact when you need to reference another sheet that's the recommended method.

    What formulas did you try?

    If you select your whole range, i.e. sheet1!A1:AA200 with A1 the active cell (i.e. select A1 first) then just use this formula for MALE

    =COUNTIF(MALE,A1)

    and similar for FEMALE

  3. #3
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845
    Hi,

    This a a simple code. Paste to a module.

    Sub test()
    Application.ScreenUpdating = False
    Dim myname As String
    Dim cel As Range, mcell As Range, fcell As Range, male As Range, female As Range
    For Each cell In Range("A1").CurrentRegion
        If cell.Text <> "" Then
            myname = cell.Text
                With Sheets("Sheet2")
                For Each mcell In Sheets("Sheet2").Range("male")
                    If mcell.Text = myname Then
                        cell.Interior.ColorIndex = 4
                    End If
                Next mcell
                For Each fcell In Sheets("Sheet2").Range("female")
                    If fcell.Text = myname Then
                        cell.Interior.ColorIndex = 3
                    End If
                Next fcell
                End With
        End If
            
    Next cell
    End Sub
    Charles

    There are other ways to do this, this is but 1 !
    Be Sure you thank those who helped.
    IF YOU'RE SATISFIED BY ANY MEMBERS RESPONSE TO YOUR ISSUE PLEASE USE THE STAR ICON AT THE BOTTOM LEFT OF THE POST UNDER THEIR NAME.

  4. #4
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Talking

    daddylonglegs,

    Thanks for your response.

    If you select your whole range, i.e. Sheet1!A1:AA200 with A1 the active cell (i.e. select A1 first) then just use this formula for MALE

    =COUNTIF(MALE,A1)

    and similar for FEMALE
    That was the first code I tried...and it worked, except for one thing.....

    Each BLANK cell in range A1:AA200 was formatted RED, since Condition 1 was being met...(due to one or more BLANK cells in MALE).


    I need cells to retain their original format if no match exists in MALE or FEMALE...or if there is no name in a particular cell (i.e.: Sheet1!A1 is blank).


    Charles,

    Your VBA looks like something I can work with. I think VBA is the way to go, anyhow....since I want the formatting to work in the event that any conditional formatting is inadvertently wiped out by the user.


    Thank you both, for your excellent replies.

  5. #5
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845
    Thanks for the feedback.

  6. #6
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,694
    Quote Originally Posted by HuskerBronco
    Each BLANK cell in range A1:AA200 was formatted RED, since Condition 1 was being met...(due to one or more BLANK cells in MALE).
    If cells are completely blank then the formula I suggested shouldn't result in them being formatted. If your cells contain "formula blanks", i.e. "" returned by a formula then you can avoid formatting with a simple tweak to the above, i.e.

    =COUNTIF(Male,A1)*(A1<>"")

  7. #7
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167
    Hey guys....I hope you are still there...!!

    You, first, Charles....

    I Rt-clicked the sheet tab for Sheet1 one of my spreadsheet and entered your VBA. (I added the missing "L" in this line of your code, first):

    Dim cel As Range, mcell As Range, fcell As Range, male As Range, female As Range
    Next, I ran the VBA...and everything seemed to work flawlessly.
    Sheet1 quickly became a grid of GREEN and RED cells...(with a few unformatted cells for names not found in either list).

    I wanted to test the VBA by entering a few names, and deleting a few names from both MALE and FEMALE ranges on Sheet2. After doing so, I clicked back to Sheet1 to see if the cell formatting had changed. It had NOT!!! The cells were the SAME color as they were after I initially ran the Sub. So, I went to Sheet2, and deleted the ENTIRE list of names in the MALE range. I expected that there would be NO FORMATTING in the cells containing MALE names....but there was!!! In fact, the formatting had not changed in ANY of the cells on Sheet1.

    Also, I have a macro that sorts Sheet1 alphabetically. After running this macro, all cells retained the formatting from the original sortation. (Meaning...if cell A1 had been formatted GREEN, it remained GREEN even if the name in that cell had changed to a FEMALE name.

    At this point, I figured that I must be doing something wrong. I began to assume that I needed some kind of event to trigger this VBA, but I had no clue how to trigger it. In an attempt to get Sheet1 freshly formatted, I opened the VBE and triggered the VBA from there.

    This time.... ....I got an error message:

    Run-time error '1004':
    Application-defined or object-defined error


    What am I doing wrong, here.....???


    daddylonglegs,

    Yes, some of the cells in the MALE and FEMALE ranges are "formula blanks".
    As I mentioned earlier...I initially had formulas identical to yours as Condition 1 and Condition 2 of my conditional formatting. This meant that any BLANK cells in the conditionally formatted range would turn RED...because they were meeting Condition 1.

    I changed the formulas in the manner that you suggested:

    =COUNTIF(Male,A1)*(A1<>"")
    And when I clicked OK....NOT ONE of the cells on Sheet1 was formatted...!!!
    And furthermore, when I tried to return to the original formulas...(get this...!!!)...there was STILL NO FORMATTING on Sheet1. And now, for some reason or other...I can't even get those OLD conditional formats to work.

    What could possibly be the problem...???
    Last edited by HuskerBronco; 01-14-2008 at 06:09 PM.

  8. #8
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845
    The color index on sheet 1 will not change even if the names within the list on sheet1 changes . You set the code to say if the name in either ranges are not found then the color index changes to what ever you say.

  9. #9
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Talking

    Hello Charles....

    Sorry it took me so long to get back to you.
    I attached your latest code to the end of my SORT macro...and it worked exactly as planned.

    In your last reply, you stated:

    If neither is found the sheet1 name will remain without color.(unless you added a name on sheet1 that had the cell fromatted for either cat. and that name was not in sheet2 the color will remain)
    This was exactly the case...and something that had to be corrected, since the names in the cells on Sheet 1 were constantly being changed each time the SORT macro was run.

    My simple solution was to add the following code AHEAD of your code...to clear the formatting prior to your code's execution.

      Range("A1:AA200").Select
        With Selection.Font
            .FontStyle = "Regular"
          Selection.Interior.ColorIndex = xlNone
        End With
    The unfortunate side effect of all of this...??? ...My SORT macro now takes longer to run...(about 10-12 seconds).
    Since I have ScreenUpdating set to FALSE on the macro...I now need to figure out how to attach a STATUS BAR to the macro so that the user doesn't feel like nothing is happening, or that the program has hung.

    Going to get to work on that, right now.

    Thanks a bunch for your help with this.
    I've learned a great deal more about VBA from this exercise.

  10. #10
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845
    Hi,


    I don't think it's necessary for the code to set Font style to Regular.
    The below code may be a little faster.

    
    Range("A1:AA200").Interior.ColorIndex = xlNone

  11. #11
    Forum Contributor
    Join Date
    01-12-2004
    Location
    Nebraska, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Arrow

    Hello, again, Charles....

    In your last reply, you stated the following:

    I don't think it's necessary for the code to set Font style to Regular.
    I failed to mention that my SORT macro sets Sheet1 matches to bold.
    (Actually, I modified YOUR code to do so...before I added it to my SORT macro).
    Therefore, I needed to reset the font prior to running your new code.

    I considered leaving the font 'Regular' throughout the sort process...to speed up the macro.
    After removing that code...and removing code to reset the font to 'Regular'...
    the macro execution time wasn't noticeably diminished.

    Perhaps I could reply with the exact code I am using in my workbook...and you might see other ways that I might speed up this macro. I am sure, due to my inexperience with VBA, that some of my code is redundant...or even completely unnecessary.

    Would you be interested in glancing at this code, and offering your advice??

    I thank you for the help that you have given me, so far. The educational value of this forum is "off the charts". I have yet to post a question that couldn't be answered to my satisfaction. I can't thank you enough for the time and thoughtfulness that you put into this forum.

    My thanks go out to the following moderators and registered users, as well:

    VBA Noob - kraljb - duane - Bryan Hessey - Special-K - JR@SGC - tony h - jtp - davesexcel - Carim - MSP77079 - vane0326 - Dav - Clayton Lock - rylo - dominicb - Norie - downforce - Leith Ross - Mikerickson - ChemistB - Aladin Akyurak - and last but not least...daddylonglegs.

+ 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