目录
  1. 1. 宏代码源码
Excel单元格RGB色号填背景色

  对Excel单元格中内容为如255,0,255样式的RGB色值,按单元格中的色值设置该单元格的背景色。修改方式为宏代码,提供的宏代码支持对表中A列的所有单元格背景色按相应单元格中的RGB色值进行设置;

示例

宏代码源码

&#8195使用下面宏代码对单元格RGB色值进行背景填充,下列的A为所在列,可按需进行修改;RGB单元格中内容需使用英文,隔开;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Sub RGB_颜色修改()

Dim i As Long
Dim lastRow As Long
Dim rgbValues() As String
Dim totalRgbValues As Long
Dim currentRgbValues(3) As Long
Dim k As Long

' Find the last row with data
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

' Go through each cell in column A
For i = 1 To lastRow

' Assume the value in each cell is in the form "R1,G1,B1"
rgbValues = Split(Cells(i, "A").Value, ",")
totalRgbValues = UBound(rgbValues) + 1 ' +1 for the empty string at the end

' Check if the value has the expected form
If totalRgbValues = 3 Then
For k = 0 To totalRgbValues - 1
currentRgbValues(k) = CLng(rgbValues(k)) ' Convert to Long to handle possible large values
If k = 2 Then ' We want to set the color only after the last value is read
Cells(i, "A").Interior.Color = RGB(currentRgbValues(0), currentRgbValues(1), currentRgbValues(2))
End If
Next k
End If
Next i
End Sub
文章作者: 嗜血星空earth
文章链接: http://sxxkearth.github.io/2023/10/12/Excel%E5%8D%95%E5%85%83%E6%A0%BCRGB%E8%89%B2%E5%8F%B7%E5%A1%AB%E8%83%8C%E6%99%AF%E8%89%B2/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请附以署名及出处!

评论