==标题==
ExcelVBA编程输出ColorIndex对照色谱、RGB、16进制 |
==正文==
解决问题 |
ExcelVBA编程输出ColorIndex对照色谱 新增:RGB(R,G,B)、16进制 |
#解决思路
#1.先确定要输出的形式,标题行,标题为Array("Color Index", "Name", "Color", "R", "G", "B", "16进制")如果标题增加,数据输出自动增加 #2.Color列用 interor.ColorIndex属性 #3.RGB的数据要用 interor.Color属性的值进行运算转化 #4.16进制的数据用RGB三个数据进行转化,用VBA中的Hex,如果是Excel函数是用DEC2HEX,语法是 str = dec2hex(d, n) |
#书接上一回
先看上一篇文章: |
#修改进化,效果图(部分)
PS:比上一篇文章多也RGB(R,G,B)、16进制
#完整代码
Sub Clear清除当前表() ActiveSheet.Cells.ClearEnd Sub'ExcelVBA编程输出ColorIndex与对应颜色色谱和RGB值Sub CreateColorIndexAndColor()Dim rowN As IntegerDim colN As IntegerDim row_i As IntegerDim col_i As IntegerDim rng As RangeDim partCol As IntegerDim partRow As IntegerDim R As RangeDim Num As IntegerDim ColorName As StringDim ColorArr As VariantDim NameArr As VariantNameArr = Array("Color Index", "Name", "Color", "R", "G", "B", "16进制")ColorName = "黑色、白色、红色、鲜绿色、蓝色、黄色、粉红色、青绿色、深红色、绿色、深蓝色、深黄色、装咽火、青色、灰-25%、灰-50%、海螺色、梅红色、象牙色、浅青绿、深紫色、珊瑚红、海蓝色、冰蓝、深蓝色、粉红色、黄色、青绿色、紫罗兰、深红色、青色、蓝色、天蓝色、浅青绿、浅绿色、浅黄色、淡蓝色、玫瑰红、淡紫色、茶色、浅蓝色、水绿色、酸橙色、金色、浅橙色、橙色、蓝-灰、灰-40%、深青、海绿、深绿、橄榄绿、棕色、梅红色、靛蓝、灰-80%"Num = 56colN = UBound(NameArr) + 1'修改为随数组变化而变化的数据rowN = 1row_i = 0col_i = 0partCol = 4partRow = Num / 4ColorArr = Split(ColorName, "、")With ActiveSheet For Each R In .Range("A1").Resize(1, partCol * colN) R.value = NameArr((R.Column - 1) Mod colN) Next With .Range("A1").Resize(1, partCol * colN) .Font.Bold = True End With For i = 1 To Num Set rng = .Cells(row_i + rowN + 1, col_i + 1) rng = i 'ColorIndex rng.Offset(0, 1).value = ColorArr(i - 1) 'Name rng.Offset(0, 2).Interior.ColorIndex = i 'Color '新增代码================== ColorTemp = rng.Offset(0, 2).Interior.Color rN = ColorTemp Mod 256 gN = ColorTemp \ 256 Mod 256 bN = ColorTemp \ 256 \ 256 Mod 256 rgb_16 = RGBtoHex(CInt(rN), CInt(gN), CInt(bN)) rng.Offset(0, 3).value = rN 'R rng.Offset(0, 4).value = gN 'G rng.Offset(0, 5).value = bN 'B rng.Offset(0, 6).value = rgb_16 '"16进制" '新增代码=======end=========== row_i = row_i + 1 If i Mod partRow = 0 Then row_i = 0 col_i = col_i + colN End If Set rng = Nothing Next With .UsedRange .Font.Size = 10 .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = True .ColumnWidth = 7 .EntireColumn.AutoFit .RowHeight = 30 .Borders.LineStyle = 1' .Offset(1).WrapText = False' .Offset(1).ShrinkToFit = True End WithEnd WithEnd Sub‘RGB转16进制函数,返回stringFunction RGBtoHex(R As Integer, G As Integer, B As Integer) As String RGBtoHex = "#" & Right("0" & Hex(R), 2) & Right("0" & Hex(G), 2) & Right("0" & Hex(B), 2)End Function
#完整效果图
#相关链接
在VBA中,Interior.ColorIndex属性及其应用,举例说明
==正文结束==
==更多合集==
===***===
关注 | 转发 | |
点赞 | 在看 |