Originally Posted by
Cyberdude
I'm not sure why I'm struggling with this, but I just can't figure it out. I want to use an Inputbox to ask the user for a color index number. He also can reply by clicking Cancel.
I can't figure out whether he has replied with a zero (valid value) or pressed the Cancel button.
Hey Dude,
Test for "False"
Sub SetTabColor()
Dim TabColor As Variant, Msg$
Msg = "Enter the sheet tab new color:"
TabColor = Application.InputBox(Msg, Title, Default:=0, Type:=5) 'Number or boolean
If TabColor = "False" Then GoTo Finish 'Cancel
' unrelated logic here
Finish:
End Sub
But I must say this is a singularly unfriendly way of getting a colour. Here is a routine that is a tad friendlier
Sub SetTabColor()
Debug.Print GetColorindex
Finish:
End Sub
Function GetColorindex(Optional Text As Boolean = False) As Long
Dim rngCurr As Range
Set rngCurr = Selection
Application.ScreenUpdating = False
Range("IV1").Select
Application.Dialogs(xlDialogPatterns).Show
GetColorindex = ActiveCell.Interior.ColorIndex
If GetColorindex = xlColorIndexAutomatic And Not Text Then
GetColorindex = xlColorIndexNone
End If
ActiveCell.Interior.ColorIndex = xlColorIndexAutomatic
rngCurr.Select
Set rngCurr = ActiveSheet.UsedRange
Application.ScreenUpdating = True
End Function