Results 1 to 7 of 7

Thread: Colouring Excel Cells

  1. #1

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575

    Colouring Excel Cells

    Hi,

    I need to be able to select a range of cells, and then press a button in a toolbox I have created.

    When you press the button, the code goes through every cell selected, gets its value and changes its ColorIndex to the value in the cell.

    But I have a problem...

    I currently have this code:

    Selection.Interior.ColorIndex = ActiveCell.Value

    which works okay for singular cells. But if you select a range of more than 1 cell, it colours the entire range by the first number in it.

    Can anyone help me get it so it will go through each cell individually and change each one to its appropiate colour?

    Thanks very much! This will be a real help, I'm not sure how I would code such a thing yet.
    I have never used "For" before which will probably be needed? And I'm not very good at arrays if those are needed either. Thanks again

  2. #2
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    HI,
    By you description I gave it a try an it worked ok for me. If you can send me you workbook and the code you are using so I can see your problem and try to help you find a solution.


    Jason

  3. #3

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    okay...

    if u got cells like this

    1
    2
    3
    4

    and select the cells, 1 > 4 so they are all highlighted, then press the button, all of them go black which is the colour of 1

    each cell doesn't go the individual colour of each value.

    you sure thats what you thought i meant?

  4. #4
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    Just run through the cells with a loop.
    VB Code:
    1. For i = 1 To 1000
    2.   If Range("$A$" & i).Text = "" Then Exit Sub
    3.  
    4.      Range("$A$" & i).Interior.Color = Range("$A$" & i).Text
    5. Next

  5. #5

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    lol sorry but i have no idea how that was suppost to have worked....

    either got an error with the code about it not being able to set its color index, or it would just colour the entire range the first colour again... or do nothing at all which happened most of the time...

    there has to be a simple way about this. I don't understand why you have this "$A$" business...

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    I wrote this a while ago to round numbers in a highlighted range.
    I know you can modify it to change the color instead of rounding.
    It works better to use the IsEmpty rather than the = ""
    VB Code:
    1. Private Sub Workbook_Open()
    2.  
    3.     Dim i As Integer
    4.     Dim ii As Integer
    5.    
    6.     areaCount = Application.Selection.Areas.Count
    7.     If areaCount <= 1 Then
    8.         MsgBox "The selection contains " & Application.Selection.Columns.Count & " columns."
    9.         MsgBox "The selection contains " & Application.Selection.Rows.Count & " rows."
    10.     Else
    11.         For i = 1 To areaCount
    12.             MsgBox "Area " & i & " of the selection contains " & Application.Selection.Areas(i).Columns.Count & " columns."
    13.             MsgBox "Area " & i & " of the selection contains " & Application.Selection.Areas(i).Rows.Count & " rows."
    14.         Next i
    15.     End If
    16.     Set xRange = Application.ActiveCell.CurrentRegion
    17.     c = xRange.Columns(xRange.Columns.Count).Column
    18.     c = Chr(64 + c)
    19.     b = xRange.Columns(1).Column
    20.     b = Chr(64 + b)
    21.     MsgBox "First column = " & b
    22.     MsgBox "First row = " & xRange.Rows(1).Row
    23.     MsgBox "Last column = " & c
    24.     MsgBox "Last row = " & xRange.Rows(xRange.Rows.Count).Row
    25.     For i = xRange.Rows(1).Row To xRange.Rows(xRange.Rows.Count).Row
    26.         For ii = xRange.Columns(1).Column To xRange.Columns(xRange.Columns.Count).Column
    27.             b = xRange.Columns(ii).Column
    28.             b = Chr(64 + b)
    29.             CellValue = Worksheets("Sheet1").Range(b & i).Value
    30.             If IsEmpty(CellValue) = False Then
    31.                 Worksheets("Sheet1").Range(b & i).Value = Round(CellValue, 0)
    32.             End If
    33.         Next
    34.     Next
    35.    
    36. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Member
    Join Date
    May 2004
    Location
    Where i am
    Posts
    38
    Okay Lithia,

    Private Sub CommandButton1_Click()
    On Error GoTo ErrorHandler
    If ActiveCell.Value = 1 Then a
    If ActiveCell.Value = 2 Then b
    If ActiveCell.Value = 3 Then c
    If ActiveCell.Value = 4 Then d
    Exit Sub
    ErrorHandler: MsgBox "Mean Program"
    End Sub
    Private Sub a()
    If ActiveCell.Value = 1 Then ActiveCell.Value = 3
    Selection.Interior.ColorIndex = ActiveCell.Value
    If ActiveCell.Value = 3 Then ActiveCell.Value = 1
    End Sub

    Private Sub b()
    If ActiveCell.Value = 2 Then ActiveCell.Value = 44
    Selection.Interior.ColorIndex = ActiveCell.Value
    If ActiveCell.Value = 44 Then ActiveCell.Value = 2
    End Sub

    Private Sub c()
    If ActiveCell.Value = 3 Then ActiveCell.Value = 8
    Selection.Interior.ColorIndex = ActiveCell.Value
    If ActiveCell.Value = 8 Then ActiveCell.Value = 3
    End Sub

    Private Sub d()
    If ActiveCell.Value = 4 Then ActiveCell.Value = 5
    Selection.Interior.ColorIndex = ActiveCell.Value
    If ActiveCell.Value = 5 Then ActiveCell.Value = 4
    End Sub

    Oh biology next.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width