Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2008

Tip: Looking for answers? Try searching our database.

Interior.Color from a function yields Error 1004

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Diaren Whan - 23 Jan 2008 02:06 GMT
In Excel 2007 I am trying to change the Fill color of a cell from a macro. I
am fairly adept with excel macros. So here is the problem with a simple
example.

Sub FormatResult()
 Range("A1").Interior.Color = 255  '*** This Works
End Sub

Add a formula to a cell e.g. =FormatResultByRange(A1)
Function FormatResultByRange(r as Range)
 r.Interior.Color = 255 '*** Error 1004
End function

alternatively...
Function FormatResultByRange(r as Range)
 ' r is ignored and not used
 Range("A1").Interior.Color = 255 '*** Error 1004
End function

It doesn't seem to work when executed via a formula.

Code In Context:
I know the follwoing example can be achieved using coditional formatting,
but the real code is actually very complex involving 3d math etc. But what is
below illustrates the problem in context.

n.b. cell formula:  = FormatAge()
Function FormatAge()
 Dim r as Range
 Dim age as Integer
 Dim fmtcolor as Variant
 Set r = Application.Caller
 age = r.Value
 if(age<16) then
    fmtcolor = &H0000FF
 else
    if(age<18) then
       fmtcolor=&HFF8000
    else
         fmtcolor=&H00FF00
    end if
 end if
 r.Interior.Color = fmtColor '*** Error 1004
End Function
JP - 23 Jan 2008 03:51 GMT
You've discovered one of the limitations of functions -- a UDF cannot
do anything to the worksheet.

A function can return a value as the result of a calculation, but you
can never do something that would affect the worksheet such as
changing colors,

Check out this KB: http://support.microsoft.com/kb/170787

HTH,
JP

On Jan 22, 9:06 pm, Diaren Whan <Diaren
W...@discussions.microsoft.com> wrote:
> In Excel 2007 I am trying to change the Fill color of a cell from a macro. I
> am fairly adept with excel macros. So here is the problem with a simple
[quoted text clipped - 40 lines]
>   r.Interior.Color = fmtColor '*** Error 1004
> End Function
Diaren Whan - 23 Jan 2008 11:52 GMT
If that is the case, then I find it strange that the following works from a  
UDF:

Range("A1").Font.Color = 255

Nevertheless the above is what has been used until a better solution comes up.

Thanks for your time.

> You've discovered one of the limitations of functions -- a UDF cannot
> do anything to the worksheet.
[quoted text clipped - 54 lines]
> >   r.Interior.Color = fmtColor '*** Error 1004
> > End Function
JP - 23 Jan 2008 12:45 GMT
I tried it but it didn't work on my machine no matter what combination
I used.

r.Interior.Color
r.Font.Color
Range("A1").Interior.Color
Range("A1").Font.Color

Can you explain what you are trying to do with the formula? Maybe
someone could be of further assistance.

HTH,
JP

On Jan 23, 6:52 am, Diaren Whan <DiarenW...@discussions.microsoft.com>
wrote:
> If that is the case, then I find it strange that the following works from a  
> UDF:
[quoted text clipped - 4 lines]
>
> Thanks for your time.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.