简体   繁体   English

更改呼叫单元内部颜色

[英]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. 我正在尝试在Excel中创建一个非常简单的VBA函数,用于计算特定值(基于另一个单元格内容)并设置调用单元格值和内部颜色。 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. 无论如何,我已经看到一些其他SO帖子谈论类似的变化(例如这里 ),但他们的解决方案似乎并不适合我。 I would rather not do this with conditional formatting because I want something that I can easily transfer between different Excel files. 我宁愿不使用条件格式化这样做,因为我想要一些我可以轻松地在不同的Excel文件之间传输的东西。

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 请参阅: 在Excel中使用UDF更新工作表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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