简体   繁体   中英

Change Calling Cell Interior Color

I'm trying to create a very simple VBA function in Excel that calculates a specific value (based on another cells contents) and sets the calling cells value and interior color. I'm fine with the value calculation, but it's the interior coloring that is throwing me for a loop.

I can do the following to set the text and font color:

Function Test()
    Application.Caller.Font.ColorIndex = 3
    Test = "Hello"
End Function

But I'd rather set the cell interior color. I've tried a couple of different iterations of the code below, but this always gives me a value error in the calling cell.

Function Test()
    Application.Caller.Interior.ColorIndex = 3
    Test = "Hello"
End Function

Anyway, I've seen some other SO posts that talk about similar changes (Eg here ), but their solutions don't seem to work for me. I would rather not do this with conditional formatting because I want something that I can easily transfer between different Excel files.

With both of these in a regular module:

Sub ChangeIt(c1 As Range)
    c1.Interior.ColorIndex = 3
End Sub

Function Test()

    With Application.Caller        
        .Parent.Evaluate "Changeit(" & .Address(False, False) & ")"
    End With
    Test = "Hello" 

End Function

See: Using a UDF in Excel to update the worksheet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM