我正在嘗試創建一個宏,其中一系列單元格背景顏色更改為預定義 RGB 顏色串列中的顏色。每次運行宏時,單元格區域的顏色都會更改為串列中的下一種顏色。
我在單元格“A9”中有一個參考編號,用于設定顏色串列中的編號(定義為 colourSel),以及它下方的 5 種 RGB 顏色串列(A10:A14)。選擇最后一種顏色后, colourSel 變數將重置并且串列從頭開始重新開始。我可以很好地回圈使用顏色,但我無法使用 Interior.Color 設定顏色變數 (colourSet)
A9 3 (ColorSel) A10 RGB(131, 99, 172) A11 RGB(240, 92, 106) A12 RGB(13, 176, 219) A13 RGB(231, 167, 35) A14 RGB(244, 130, 33)
我收到錯誤型別不匹配,我認為是因為 colourSet 被定義為String
Dim colourSel As Integer
Dim colourSet As String
Range("a9").Select
If ActiveCell.Value > 5 Then
colourSel = 1
GoTo resetcolour
End If
colourSel = Range("a9").Value
resetcolour:
Range("a9").Offset(colourSel, 0).Select
colourSet = ActiveCell.Value
colourSel = colourSel 1
Range("a9").Value = colourSel
Range("b2:c3").Interior.Color = colourSet
uj5u.com熱心網友回復:
您可以在 VBA 中使用此功能來執行轉換...
Public Function ConvertRGBStringToValues(strRGB As String)
Dim arrRGB As Variant
' This is an example of what can be passed into this function ...
' strRGB = "RGB(11, 105, 11)"
arrRGB = Split(Replace(Replace(Replace(strRGB, "RGB", "", Compare:=vbTextCompare), ")", ""), "(", ""), ",")
ConvertRGBStringToValues = RGB(arrRGB(0), arrRGB(1), arrRGB(2))
End Function
...所以你會像這樣應用它...
Range("b2:c3").Interior.Color = ConvertRGBStringToValues(colourSet)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/416002.html
標籤:
