求excel宏代码高手帮忙

excel根据单元格内容的颜色,得到指定的值。写个自定义函数

一、执行“工具→宏→Visual Basic编辑器”菜单命令(或按“Alt+F11”快捷键),打开Visual Basic编辑窗口。
二、在窗口中,执行“插入→模块”菜单命令,插入一个新的模块--模块1。
三、在右边的“代码窗口”中输入代码:

Function testcolor(rng)
    If rng.Font.ColorIndex = 23 Then
        testcolor = -1
    Else
        If rng.Font.ColorIndex = 3 Then
            testcolor = 1
        Else
            testcolor = 0
        End If
    End If
End Function

四、关闭窗口,自定义函数完成。

五、在结果单元格输入自定义函数,如上图。

测试通过。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-06-14

Function colortonum(rn As Range) '颜色转值

Dim kk As Long

kk = rn.Font.Color

If kk = 255 Then

    colortonum = 1

ElseIf kk = 12611584 Then

    colortonum = -1

ElseIf kk = 5287936 Then

    colortonum = 0

Else

    colortonum = 10000

End If

End Function

excel中,alt+F11,打开VBE,用菜单:插入\模块,将代码贴入;

在excel中象用一般函数一样用:=colortonum(一个单元格地址)即可。

说明:只针对您的红绿蓝进行判断的。如果是其它颜色返回10000,当然您可改代码中的10000这个值。

供参考,祝顺利。

相似回答