+ Reply to Thread
Results 1 to 7 of 7

Thread: Autoselect Cells Based on Cell value

  1. #1
    Registered User
    Join Date
    11-22-2008
    Location
    Brisbane, Australia
    MS-Off Ver
    2003
    Posts
    23

    Autoselect Cells Based on Cell value

    Hi there I need a macro that will select the value in lets say A1 and then perform a cell shade on the value of A1 to that many rows. Now I found this,
    Sub ShadeEveryOtherRow()
        Dim Counter As Integer
    
        'For every row in the current selection...
        For Counter = 1 To Selection.Rows.Count
            'If the row is an odd number (within the selection)...
            If Counter Mod 2 = 1 Then
                'Set the pattern to xlGray16.
                Selection.Rows(Counter).Interior.Pattern = xlGray16
            End If
        Next
    
    End Sub
    but that only works on what the user selects with the mouse.

    I want it to work based on what the cell value says, so if a1 = 3 then I want 3 cells to be shaded.
    it has to work from a number of cells ranging from A1 through to AA2.

    IE,

    Book1.xls
    Last edited by royUK; 11-23-2008 at 03:00 AM. Reason: add code tags

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226
    I'm not sure of the row selection code, but the code you need to make the formatting gray for every OTHER row in an already selected range is:
        Selection.FormatConditions.Delete
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=MOD(ROW(),2)=0"
        Selection.FormatConditions(1).Interior.ColorIndex = 15
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639
    merilvingian

    Please take the time to read he Forum Rules & use Code Tags in future.

    If A1 contains 5 do you want five rows shaded and where do you want he shading to start?
    Last edited by royUK; 11-23-2008 at 03:15 AM.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  4. #4
    Registered User
    Join Date
    11-22-2008
    Location
    Brisbane, Australia
    MS-Off Ver
    2003
    Posts
    23
    Quote Originally Posted by royUK View Post
    merilvingian

    Please take the time to read he Forum Rules & use Code Tags in future.

    If A1 contains 5 do you want five rows shaded and where do you want he shading to start?
    Sorry I did not read your complete post. Sorry.

    um if A1 contains 5 I want the shading to start directly below A1, so on A2 5 rows down but every second row. If you have a look at the attachment you will see what I am aiming for. I just used the macro I have for now and manually selected the rows.

    Thanks in advance.

  5. #5
    Registered User
    Join Date
    11-22-2008
    Location
    Brisbane, Australia
    MS-Off Ver
    2003
    Posts
    23
    Any Ideas people? This is still unresolved.

  6. #6
    Valued Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    639
    How many different colors do you need?

  7. #7
    Valued Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    639
    Play with this and see if it will work.
    Sub in the values you need Here
    If Cell.Value = "3" and then
    here:
    Cell.Value <> "3"
    It is setup to work on column A , Rows 1-800
    Private Sub Worksheet_Change(ByVal Target As Range)
    Set MyPlageSheet2 = Range("A1:A800")
    
        For Each Cell In MyPlageSheet2
        
            If Cell.Value = "3" Then
                Cell.Resize(, 4).Interior.ColorIndex = 3
            End If
            If Cell.Value = "4" Then
                Cell.Resize(, 4).Interior.ColorIndex = 4
            End If
            If Cell.Value = "43" Then
                Cell.Resize(, 4).Interior.ColorIndex = 43
            End If
            If Cell.Value = "36" Then
                Cell.Resize(, 4).Interior.ColorIndex = 36
            End If
            If Cell.Value = "40" Then
                Cell.Resize(, 4).Interior.ColorIndex = 40
            End If
            If Cell.Value = "45" Then
                Cell.Resize(, 4).Interior.ColorIndex = 45
            End If
            If Cell.Value = "42" Then
                Cell.Resize(, 4).Interior.ColorIndex = 42
            End If
              If Cell.Value = "2" Then
                Cell.Resize(, 4).Interior.ColorIndex = 2
            End If
    If Cell.Value <> "3" And Cell.Value <> "4" And Cell.Value <> "43" And Cell.Value <> "36" And Cell.Value <> "40" And Cell.Value <> "45" And Cell.Value <> "42" And Cell.Value <> "2" Then
            Cell.Interior.ColorIndex = xlNone
            End If
            
        Next
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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.2.0