Consulting

Results 1 to 5 of 5

Thread: Get value of FillColour button

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Get value of FillColour button

    I use this simple code to toggle cell fill colour
    [VBA]Sub Yellow()
    If Selection.Interior.ColorIndex = 6 Then
    Selection.Interior.ColorIndex = xlNone
    Else
    Selection.Interior.ColorIndex = 6
    End If
    End Sub[/VBA]
    How do I change it to use the last colour set by the FillColour button? I see it "remembers" the last colour used.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Perhaps
    [VBA]Public LC as Long
    Sub Yellow()
    LC= Selection.Interior.ColorIndex
    If Selection.Interior.ColorIndex = 6 Then
    Selection.Interior.ColorIndex = xlNone
    Else
    Selection.Interior.ColorIndex = 6
    End If
    End Sub
    [/VBA]you can then use LC in any code/function, have i missed the mark on what you are looking for?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Malcolm,

    The fill colour index is not exposed in the OM, and there is no event to tap into.

    This question was asked a while back in OzGrid, and I came up with this technique, using the TooltipText and parses it. I have added all colours, I leave it to yoiu to complete

    [vba]

    Function GetColourIndex()
    Dim sText As String
    Dim iPos As Long
    Dim iColorIndex As Long

    sText = CommandBars("Formatting").Controls("&Fill Color").TooltipText
    iPos = InStr(sText, "(")
    If iPos > 0 Then
    sText = Mid(sText, iPos + 1, InStr(sText, ")") - iPos - 1)
    Select Case sText
    Case "Automatic": iColorIndex = xlColorIndexNone
    Case "Black": iColorIndex = 1
    Case "White": iColorIndex = 2
    Case "Red": iColorIndex = 3
    Case "Bright Green": iColorIndex = 4
    Case "Blue": iColorIndex = 5
    Case "Yellow": iColorIndex = 6
    Case "Pink": iColorIndex = 7
    Case "Turquoise": iColorIndex = 8
    Case "Green": iColorIndex = 10
    Case "Dark Blue": iColorIndex = 11
    Case "Dark Yellow": iColorIndex = 12
    Case "Violet": iColorIndex = 13
    Case "Teal": iColorIndex = 14
    Case "Gray - 25%": iColorIndex = 15
    Case "Gray - 50%": iColorIndex = 16
    Case "Light Turquoise": iColorIndex = 20
    Case "Dark Red": iColorIndex = 30
    Case "Light Blue": iColorIndex = 41
    Case "Orange": iColorIndex = 46
    Case "Sea Green": iColorIndex = 50
    Case "Brown": iColorIndex = 53
    End Select
    GetColourIndex = iColorIndex
    End If
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Very clever. Thanks Bob.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Something like this can be used. In the referenced link, there is an API method but choosing a custom color will fail. ColorIndex is not as reliable as Color.
    [VBA]'http://www.mrexcel.com/forum/showthread.php?t=370318
    Sub SetTabColor()
    Dim r As Range, rc As Long
    Set r = Range("h100")
    r.Select
    rc = r.Interior.Color
    With Application.CommandBars("Fill Color")
    .Visible = True
    .Position = msoBarFloating
    .Left = r.Offset(0, 1).Left
    .Top = r.Top - r.Top
    Do
    DoEvents
    Loop Until .Visible = False
    End With
    ActiveSheet.Tab.ColorIndex = r.Interior.ColorIndex
    r.Interior.Color = rc
    End Sub[/VBA]

Posting Permissions

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