Consulting

Results 1 to 3 of 3

Thread: Error when setting Interior ColorIndex

  1. #1
    VBAX Contributor Glaswegian's Avatar
    Joined
    Sep 2004
    Location
    Glasgow, Scotland
    Posts
    196
    Location

    Error when setting Interior ColorIndex

    I'm sure there is an easy explanation for this, but I can't see it at the moment!
    All I'm trying to do is clear some cells and change the interior colour based on the value of a Control Toolbox checkbox. However, I receive an error '1004 - unable to set ColorIndex property of Interior Class'. The code is so simple

    Private Sub CheckBox1_Click()
    Dim myRange As Range
    Set myRange = Worksheets("Sheet7").Range("A1:D1")
    If CheckBox1.Value = True Then
    With myRange
    .Interior.ColorIndex = 6
    .Value = ""
    End With
    End If
    End Sub
    It works fine if I wrap the ColorIndex line in a single sub.

    Sorry if this is a bit stupid but I'm stumped!

    Thanks

    Iain - XL97 on NT4
    Iain - XL2010 on Windows 7

  2. #2
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    Hi Iain ,

    The problem is caused by the control taking focus.
    Add the line to select a cell and all should be ok.

    Private Sub CheckBox1_Click()
        Dim myRange As Range
        
        ActiveCell.Activate ' to take focus away from control
        
        Set myRange = Worksheets("Sheet1").Range("A1:C1")
        If Sheet1.CheckBox1.Value = True Then
            With myRange
                .Interior.ColorIndex = 6
                .Value = ""
            End With
        End If
    End Sub
    Cheers
    Andy

  3. #3
    VBAX Contributor Glaswegian's Avatar
    Joined
    Sep 2004
    Location
    Glasgow, Scotland
    Posts
    196
    Location
    Andy

    Of course!!

    It's always the most obvious things that catch me out...

    Many thanks.

    Regards
    Iain - XL2010 on Windows 7

Posting Permissions

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