Use an excel formula to set the cell style?

时间:2018-06-04 17:36:00

标签: excel excel-vba vba

I have a sheet named "Data" with one column of data from a DB and a second column with unique keys we type in. We have UDF, GETVALUE, that looks up the cell by key and then returns the value from the cell beside it like, =GETVALUE("VAL001").

The UDF returns a Double. The problem I'm having is that the formatting of that double can have real effects, "$0.05" means something very different than "5%", even though GETVALUE returns 0.05 in either case.

So... is there some way I can set the format on sheet one so it matches the format on the other? IE, if the value is a percent on one page, I'd like anyone referencing it to also be able to set to a percent as well. Can this be done with a UDF in the conditional formatting?

1 个答案:

答案 0 :(得分:0)

让我玩了一下,但我想出了一个解决方案。

首先,我创建了一个返回NumberFormat字符串GETFORMAT的新UDF。格式与GETVALUE格式相同。

然后我使用了这个公式的条件格式“

=IF(ISNUMBER(SEARCH("%",GETFORMAT(Data1,"THECELL")))

这将返回true或false,具体取决于格式中是否存在%。然后我将真实条件设为%,而假设为$。

这并不完美,如果你有多种风格,它可以变得非常复杂,但它适用于简单的情况。