Intermittent 1004 error trying to set font color

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
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?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try changing:
VBA Code:
'       Vault Log folder same as Thisworkbook directory flag.
        With .Range("bVault_Files_Thisworkbook_Directory").Offset(0, -1).Resize(1, 2)

to

VBA Code:
'       Vault Log folder same as Thisworkbook directory flag.
        With .Range("bVault_Files_Thisworkbook_Directory").Offset(0, -1).Resize(1, 2).address
 
Upvote 0
Scratch that...looked like it worked for me the first time and then not again...
 
Upvote 0
This worked:
VBA Code:
With Range("bVault_Files_Thisworkbook_Directory").Offset(0, -1).Resize(1, 2)

Just got rid of the . before range.
 
Upvote 0
Thank you Candyman8019 for the prompt response.

Nope, that is not the right fix. If adding address as suggested then I am not using an object so get Compile Error "With object must be user-defined type, object or variant."
 
Upvote 0
If using your second suggestion how does Excel know which worksheet to look in? My code starts with the With [Control] statement to specify that the range object is on the Control worksheet.
 
Upvote 0
I'm feeling like this is more of a range issue. Is it possible that the range "bVault_Files_Thisworkbook_Directory" changes. For example, if it were ever to include column A and then you offset -1 column, it could result in this error.
 
Upvote 0
No, that name and the cell it points to do not change. It is just a range name. And as shown above the Debug.Print statements reports the correct range address.

And, as I said, the issue is intermittent.

I had this clunkier code earlier that seemed to work...

VBA Code:
    With [Control]

'       "Use this workbook" setting and label.
        With .Range("bVault_Files_Thisworkbook_Directory")
            .Value = True

            With .Offset(0, -1).Resize(1, 2).Font
                .ThemeColor = xlThemeColorDark1
                .TintAndShade = -0.249977111117893
            End With

        End With 'range

    End With '[Control]

...but I wanted to tighten it up so I went to the offending code.

I guess I'll just have to accept that I am doing something really basic to cause the error. ****!
 
Upvote 0
Fluff yes protected UserInterfaceOnly. I suppose I could try unprotecting first?
 
Upvote 0

Forum statistics

Threads
1,215,751
Messages
6,126,668
Members
449,326
Latest member
asp123

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