+ Reply to Thread
Results 1 to 10 of 10

Changing cells colour programatically?

  1. #1
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Changing cells colour programatically?

    Hello all,

    I am trying to change all cells on a work sheet or a named range to clear (or white) using VBA, i want to be able to change a cells colour back to its normal state.

    Can anyone help?..........here's what i have so far you can run it but it does nothing!

    Simon

    Sub colourchange()
    Dim mycell
    Dim rng as Range
    'For Each mycell In rng("colourrange")
    With Worksheets("sheet1")
    Set rng = Range("A1:C7")
    For Each mycell In rng
    If rng.Interior.ColorIndex = 38 Then
    rng.Interior.ColorIndex.Format.Clear
    End If

    Next mycell
    End With
    End Sub

  2. #2
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    315
    try:

    Sub colourchange()
    Dim mycell
    Dim rng As Range

    Set rng = Range("A1:C7")
    For Each mycell In rng
    If rng.Interior.Colorindex =38 Then
    'rng.Interior.ColorIndex.Format.Clear
    rng.Interior.ColorIndex = xlNone
    End If

    Next mycell

    End Sub

  3. #3
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Didnt work!

    Well thanks for your reply, i tried your modification ran it on the sheet but like my code seemed to do nothing, am i doing something wrong?

    Simon

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Simon,

    Try this code...

    Sub colourchange()

    Dim MyCell
    Dim Rng As Range

    Set Rng = Worksheets("Sheet1").Range("A1:C7")

    For Each MyCell In Rng
    MyCell = Rng.Interior.ColorIndex
    If MyCell = 38 Then
    Rng.Interior.ColorIndex = xlNone
    End If
    Next

    End Sub


    Sincerely,
    Leith Ross

  5. #5
    Forum Expert swatsp0p's Avatar
    Join Date
    10-07-2004
    Location
    Kentucky, USA
    MS-Off Ver
    Excel 2010
    Posts
    1,545
    May I ask if you are sure the affected cells are truly color 38? Your code will only change to 'xlnone' those that are currently color38 (which is the color "Rose").

    Just a thought.

    Bruce
    Bruce
    The older I get, the better I used to be.
    USA

  6. #6
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    still didnt do it!

    I tried the code modification but i had tried something similar and it still appears to do nothing, i recorded a macro selecting the colour so that i was dead sure the colour index was right........it's driving me mad!

    All i want to do is find all the instances in a range of a set colour and turn them back to xl's normal state, it should have been simple but im making no headway, i'm using xl2003.

    Simon

  7. #7
    Forum Expert swatsp0p's Avatar
    Join Date
    10-07-2004
    Location
    Kentucky, USA
    MS-Off Ver
    Excel 2010
    Posts
    1,545
    Making sure you have a backup of your file (just in case!!), alter your code to this to see what happens:

    instead of

    Rng.Interior.ColorIndex = xlNone

    try

    Rng.Interior.ColorIndex = 0

    Does this change anything?

    Also verify the Set Rng = Worksheets("Sheetxx").Range("xx:xx") is pointed to the correct sheet name and cell range

    Good Luck!!!

    Bruce
    Last edited by swatsp0p; 05-05-2005 at 03:34 PM.

  8. #8
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Argghhhh!

    Cheers for the suggestion Bruce, tried that but to no avail, the code still appears to do nothing. I have tried it in a test book with different colours but still the same problem!

    It has to be something simple im missing.....but its a case of i cant see the wood for the trees

    Simon

  9. #9
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    ?????????

    Hi all, I have messed around with the code as below and when i run it i dont even get the message box up even though all the other cells in my test book are blank. Can anyone shed any light on this?

    Simon

    Sub colourchange()

    Dim MyCell
    Dim Rng As Range
    With ThisWorkbook.Worksheets("Sheet1")
    Set Rng = Worksheets("Sheet1").Range("A1:C7")

    For Each MyCell In Rng
    MyCell = Rng.Interior.ColorIndex
    If MyCell = 38 Then
    'Rng.Interior.ColorIndex = 0
    MsgBox "Hi"
    ElseIf MyCell = 0 Then
    MsgBox "Bye"
    End If
    Next
    End With
    End Sub

  10. #10
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161

    Done it!

    After much swapping and changing and plenty of hair fall out i managed to tame the beast!

    Code posted below!

    Simon

    Sub colour1()
    Dim Rng As Range
    With ThisWorkbook.Worksheets("Sheet1")
    Set Rng = Worksheets("Sheet1").Range("A1:H652")
    For Each MyCell In Rng
    If MyCell.Interior.ColorIndex = 4 Then
    MyCell.Interior.ColorIndex = 38
    ElseIf MyCell.Interior.ColorIndex = 3 Then
    MyCell.Interior.ColorIndex = 6
    End If
    Next
    End With
    End Sub

+ 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