将单元格背景色设置为其包含的RGB值.如何? [英] Set cell background color to its containing RGB values. How?

查看:48
本文介绍了将单元格背景色设置为其包含的RGB值.如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我希望通过手动复制和粘贴以外的方式实现的屏幕截图.

这是材料设计颜色托盘,在Excel中看起来很棒.

如何遍历范围B2:B15并将每个单元格背景色设置为其对应的包含色?

也许是 VBA循环,以遍历垂直B单元格范围, 解析每个单元格内容,并设置单元格背景颜色单元格包含的 RGB值.

因此包含 rgb(255,235,238) 的RED-50单元变为...

  Range("B2").Interior.Color = RGB(255,235,238) 


更新!

感谢大家的帮助.以下是Google驱动器链接到您帮助我构建的Excel材质设计面板.

按Control + M 激活.

解决方案

一个无需使用正则表达式的简单解决方案:

  Sub test()昏暗范围对于范围内的每个r("B1:B10")r.Interior.Color = StringToRGB(r.Text)下一个结束子公共函数StringToRGB(s作为字符串)昏暗的零件s =中(s,5,镜头-5)s =替换(s,",")零件=分割(s,,")StringToRGB = RGB(CInt(部件(0)),CInt(部件(1)),CInt(部件(2)))结束功能 

Below is a screenshot of what I hope to achieve in a way other than manually copying and pasting.

It's the material design color pallet and it looks great in Excel.

How can I loop through range B2:B15 and set each cell background color to its corresponding containing color?

Perhaps a VBA loop to go through the vertical B range of cells, parsing each cell content, and setting the cell background-color to the RGB value that the cell contains.

So a RED-50 cell containing rgb(255, 235, 238) becomes...

Range("B2").Interior.Color = RGB(255, 235, 238)


UPDATE!

Thank you everyone for the help. Below is the google drive link to the Excel material design palette you've help me build.

Press Control + M to activate.

material.design.colors.xlsm

So ALL COLORS become themselves.

解决方案

A simple solution without resorting to regex:

Sub test()
    Dim r As Range
    For Each r In Range("B1:B10")
        r.Interior.Color = StringToRGB(r.Text)
    Next
End Sub

Public Function StringToRGB(s As String) As Long
    Dim parts
    s = Mid(s, 5, Len(s) - 5)
    s = Replace(s, " ", "")
    parts = Split(s, ",")
    StringToRGB = RGB(CInt(parts(0)), CInt(parts(1)), CInt(parts(2)))
End Function

这篇关于将单元格背景色设置为其包含的RGB值.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆