+ Reply to Thread
Results 1 to 11 of 11

Shade rows by value in one column

  1. #1
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Shade rows by value in one column

    I need help with my code. I am trying to have every other line shaded except when column 2 has “X” in it.
    If Column “B” has an X in it the row should have the yellow fill then start the every other row gray shading until column “B” has another “X” and that row is to be filled yellow.
    I get a “Type Mismatch error 13” with “If Range("B" & rng) = "X" Then” highlighted. I tried using “If Range("B" & rng).Value = "X" Then” it gives me the same error.
    I have uploaded a sample workbook with sheet1 that I have the code on that errors, and sheet2 with a sample of what I am trying to do.
    All Help is greatly appreciated!

    Here is my code:
    Please Login or Register  to view this content.
    Thanks again for any help!
    Attached Files Attached Files

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Shade rows by value in one column

    Try this and see if it does what you want.

    Please Login or Register  to view this content.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: Shade rows by value in one column

    I tried your code line and it will run but it doesn't color it yellow in rows with "X" , it just shades every other line in gray. I tried several variations but still can't get it to yellow "X" rows. I did get it to yellow rows but it was the row after the "X". do you have any other ideas? I do appreciate your help!

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Shade rows by value in one column


    As I do not understand why you use this event but anyway try this :

    PHP Code: 
    Sub Demo1()
            
    Dim R&
        
    With Me.UsedRange.Rows
               
    .Interior.ColorIndex xlNone
            
    For 2 To .Count
                With 
    .Item(R)
                    If .
    Cells(2).Value "X" Then .Interior.Color vbYellow Else _
                    
    If And 1 Then .Interior.ColorIndex 15
                End With
            Next
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  5. #5
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: Shade rows by value in one column

    Sorry I forgot to give you my latest version of the code. Also I need the rows that are yellow to stay yellow.

    Please Login or Register  to view this content.
    This code is shading the row but when you move to the next row only that row is shaded.

  6. #6
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: Shade rows by value in one column

    Marc L that works perfectly! I am very new to VBA and trying to learn as I go (I also have a few dummy books). Could you please comment what is happing in each row of your code? I have never seen the "Dim R&" before what does the "&" do or mean?
    To answer your question about why I am using an event is because I am using it on a phone list that changes and has more entries added to it all the time. If you have a better way to accomplish this I am open to all help and ideas.
    Thanks again for both yours and JLGWhiz help.

  7. #7
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: Shade rows by value in one column

    Marc L Thanks for all your help but I have found that the code fills the rows for the entire width of the sheet and I need it to fill only my active columns which are from columns "A" through "J". How can I have it fill only the width of my form columns "A" through "J"?
    I know how to set a range but don't know how to make it work with your code. All help is greatly appreciated!

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Shade rows by value in one column


    A to J ? As your original attachment has only A to C …

    The common case when explanation & attachment do not reflect the real need & workbook !
    Just add a Columns statement for the Interior property …

  9. #9
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Shade rows by value in one column

    Using MarcL code
    Please Login or Register  to view this content.
    Should fill A:J on the applicable rows.
    Last edited by JLGWhiz; 07-17-2018 at 08:07 PM.

  10. #10
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: Shade rows by value in one column

    Thank you both for your help and code. In my first post I used 3 columns "A" through "C" as an example but the real sheet uses columns "A" through "J" and that is what spurred the second question about how to limit it on the real sheet. As I said I am new to VBA so my methods and questions may seen simple to those with lots of experience so I apologize if I ask dumb questions. I am having a hard time grasping the lingo of "interior properties, object, etc." Thanks again for all the help and patience. I will be marking this one solved.

  11. #11
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Shade rows by value in one column

    The dumbest questions are those which are not asked. If you need to know something, ask. Just don't keep asking the same question over and over. We have all gone through the learning stage and I, for one, still ask questions when I don't understand something.
    regards, JLG
    Last edited by AliGW; 07-18-2018 at 12:21 PM. Reason: Unnecessary quotation removed.

+ 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: 2
    Last Post: 08-30-2017, 10:39 AM
  2. Replies: 3
    Last Post: 04-12-2013, 04:36 PM
  3. *Shade* alternating rows
    By BobJones in forum Excel General
    Replies: 9
    Last Post: 09-17-2011, 02:19 AM
  4. [SOLVED] shade hidden rows
    By TJ in forum Excel General
    Replies: 6
    Last Post: 01-18-2006, 10:45 PM
  5. shade rows
    By Manohar in forum Excel General
    Replies: 2
    Last Post: 03-31-2005, 03:06 PM
  6. [SOLVED] shade rows
    By Manny in forum Excel General
    Replies: 2
    Last Post: 03-31-2005, 02:06 PM
  7. [SOLVED] Shade rows A:J
    By Elaine in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-22-2005, 10:06 AM

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