How to highlight a cell using the hex color value within the cell?

前端 未结 6 696
清歌不尽
清歌不尽 2020-11-30 04:02

I have a spreadsheet of symbols and matching hex colors. I want to fill the cell itself (or the one next to it) with the hex color within the cell. I\'ve read a bit about \"

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 04:55

    Much simpler:

    ActiveCell.Interior.Color = WorksheetFunction.Hex2Dec(Mid$(ActiveCell.Text, 2))
    

    Mid strips off the leading "#", Hex2Dec turns the hex number into a decimal value that VBA can use.

    So select the range to process, and run this:

    Sub ColorCellsByHexInCells()
      Dim rSelection As Range, rCell As Range
    
      If TypeName(Selection) = "Range" Then
      Set rSelection = Selection
        For Each rCell In rSelection
          rCell.Interior.Color = WorksheetFunction.Hex2Dec(Mid$(rCell.Text, 2))
        Next
      End If
    End Sub
    

提交回复
热议问题