Consulting

Results 1 to 11 of 11

Thread: color index

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    color index

    hello
    i wanted to get both a cell's font colornumber as well as interior colornumber. i came out with this:

    [VBA]
    Sub findcolorfont()
    Dim x As Integer
    Dim y As Integer
    If IsEmpty(ActiveCell) Then GoTo errhandler
    x = ActiveCell.Font.ColorIndex
    y = ActiveCell.Interior.ColorIndex
    MsgBox "activecell font colornumber is - " & x & Chr(13) _
    & "activecell interior colornumber is - " & y & Chr(13), vbExclamation, "get cell's colors"
    Exit Sub
    errhandler:
    MsgBox " sorry sucker, cell is empty"
    End Sub

    [/VBA]
    questions: what about cases when there is no font but the cell has an interior color .if cell has no interior color but has a font how can i replace the -4102 color number with the word "empty".is there an alternative to font number -4105.?
    thanks
    moshe

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub findcolorfont()
    Dim x As Long
    Dim y As Long

    If IsEmpty(ActiveCell) Then GoTo errhandler
    x = ActiveCell.Font.ColorIndex
    y = ActiveCell.Interior.ColorIndex
    MsgBox "activecell font colornumber is - " & _
    IIf(x = xlColorIndexAutomatic, "automatic", x) & vbNewLine & _
    "activecell interior colornumber is - " & _
    IIf(y = xlColorIndexNone, "none", y), vbExclamation, "get cell's colors"
    Exit Sub
    errhandler:
    MsgBox " sorry sucker, cell is empty"
    End Sub
    [/vba]

  3. #3
    A cell always has a Font object and always has an Interior object, regardless of content. So your error handler does nothing (and also wasn't initialised in the first place, there is no "On Error Goto" statement)
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Jan Karel Pieterse
    A cell always has a Font object and always has an Interior object, regardless of content. So your error handler does nothing (and also wasn't initialised in the first place, there is no "On Error Goto" statement)
    He has an explicit GoTo to it on an empty cell condition.

  5. #5
    Duh, of course.

    I'm not used to using Goto's.
    So if I see a label that is named "ErrHandler", I automatically assume it is meant to trap runtime errors (which are unlickely in the code sample).
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Jan Karel Pieterse
    Duh, of course.

    I'm not used to using Goto's.
    So if I see a label that is named "ErrHandler", I automatically assume it is meant to trap runtime errors (which are unlickely in the code sample).
    I know what you mea, the message text gave it way to me

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Why even use an error handler?? ..

    [vba]Sub ShowMeTheColor()
    Dim x As Long, y As Long
    If Len(ActiveCell.Value) = 0 Then
    MsgBox "The activecell is empty."
    Else
    x = ActiveCell.Font.ColorIndex
    y = ActiveCell.Interior.ColorIndex
    MsgBox "The font color index is: " & IIf(x < 0, 0, x) & vbNewLine & _
    "The cell color index is: " & IIf(y < 0, 0, y), vbInformation, "Cell Color Info"
    End If
    End Sub[/vba]

  8. #8
    My point exactly.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not? Just because you wouldn't doesn't make it any more of an invalid approach (notwithstanding the GoTo). You could argue that it gives a more logically readable flow,

  10. #10
    Well, this could become an endless discussion. Because how one handles things like these is a matter of taste, but before all a matter of programming style.

    I use Style A, you use B and someone else might use C. Nothing wrong with either of those, as long as you produce code you can understand easily if you go back to it after some time.

    My personal preference is to use error handlers only to handle errors (as much as possible, that is).
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I don't disagree with anything you say, but as you quite rightly say, it is a matter of taste. So if the OP chooses to do it that way, that is perfectly fine IMO, and not wholly appropriate to question something just because one doesn't do it that way personally (I tried to say that as respectfully as I could )

Posting Permissions

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