Range.Interior.Color in workbook_open event causes error 1004

  • Hi all, first post, but long-time appreciator of OzGrid. Got a quirky one.


    Range(...).Interior.Color = vbRed (or any other color) during Workbook_Open sub results in Run-time error 1004, Application- or object-defined error.


    Preceding lines that reference the same cell do not, and work as expected. Code follows...

    In the above, the cell value is correctly set, but the sub fails on setting the color.


    NeutralCell is defined in a Public_Declarations module as follows:-

    Code
    Public Const NeutralCell As String = "L1"

    Debug shows NeutralCell = "L1", range is correctly set, and vbRed is 255.


    The same error occurs on the corresponding lines after Else (i.e. not InDeveloperMode). The value is cleared, and the color set gives error 1004.


    Any ideas?


    Thanks, John.

  • I can't see why you are using a variable for the range.



    It's hard to see what is wrong without seeing an example workbook but if InDeveloperMode is a Boolean when the WorkBook opens it will always be False so the code will not run

  • Hi Roy,


    Thanks for coming back so quickly. InDeveloperMode is a standard module of mine that simply determines if an XLTM is opened with 'New' or 'Open'.

    This is working fine.


    The problem is quite specific to Range(...).Interior.Color = ... in this Open_Workbook context.


    If you even have some troubleshooting tips (beyond the usual, which I have tried), I would be happy to hear of them.


    Cheers, John.

  • Maybe you need to specify the sheet


  • Many thanks again, Roy.


    I have tried this before but will try it again now.


    Yep, same result. I have tried specifying the worksheet both directly and using with - end with. Result is always the same.


    I have also used both Color and ColorIndex, but the result is also the same.


    Is there a document anywhere about the sequence in which Excel sets up the VBA environment on invocation? Maybe it's a sequencing issue?


    But then again, how come the .Value assignment on the same cell works reliably, and the .Color assignment reliably fails? And debug shows that all public declarations are set...


    Cheers, John.

  • Interestingly, it would appear that only the .Value assignment works. I have tried various others apart from .Interior.Color, such as .Font.Color, .Width, even .Text, and they all fail, although the error codes differ slightly.

  • Is the sheet protected?

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • BINGO! Rory, you've spotted it. Thank you.


    Yes, the sheet was protected. The cell in question ('NeutralCell') was unprotected, but apparently this only allows setting cell value, not other properties.


    Not sure where I missed this along the way, but glad to have it resolved.


    Props to you!


    Cheers, John.

  • Thanks for your help too, Rory. Unfortunately, it was not easy to send you the worksheet, as it is one of several in an interrelated system, and much of the data is confidential in nature.


    Apologies if I appeared to ignore your advice.


    Cheers, John.

  • If you allow formatting cells when protecting the sheet, then your code will work.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!