Intermittent 1004 error trying to set font color

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
855
Office Version
  1. 365
Platform
  1. Windows
Team Mr. EXCELlent

I am flummoxed. I have some code that merely grays out some data fields (cells) in a worksheet so user does not try to use irrelevant inputs. But SOMETIMES my seemingly simple code gives me a 1004 Application-defined or Object-defined error. I cannot figure out why my code works sometimes and not others.

Here is a portion of the code as an example. In the Control worksheet are the cells to gray out. I know that the range name is a bit clunky but that is not the issue. It seems to be related to how I am setting the font color. I added the second With statement -- for the range itself -- to try to isolate the issue. Indeed the 1004 error is from code .Font.ColorIndex = 15

VBA Code:
    With [Control]

'This reports the correct address of the range whose font color I am trying to change.
Debug.Print .Range("bVault_Files_Thisworkbook_Directory").Offset(0, -1).Resize(1, 2).Address

'       Vault Log folder same as Thisworkbook directory flag.
        With .Range("bVault_Files_Thisworkbook_Directory").Offset(0, -1).Resize(1, 2)
            .Font.ColorIndex = 15 '<= the offending code
        End With
        
    End With

The debug.print statement yields this result: $B$25:$C$25, so I know that the range itself is valid. The worksheet is protected using the UserInterfaceOnly setting.

What simple concept/mistake am I missing?
 
Whilst I've never used UserInterfaceOnly I have heard that it can be temperamental and does not work with everything.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Maybe this is my issue? From StackOverflow:

The status UserInterfaceOnly:=TRUE is unfortunately not stored in the file - therefore, if you reopen the file, it will be fully protected. Either use the Workbook_Open event to reapply this protection status - or unprotect and then reprotect the worksheet in your VBA code directly
 
Upvote 0
Solution
OK, so unprotecting then reprotecting UserInterfaceOnly when the workbook is opened seems to have fixed the issue. Thank you Fluff for the assist!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top