本文介绍了基于细胞中的价值的彩色细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一列中,我创建了一个名为ColRange的命名范围。该列的数字范围从-350到500。
如何根据单元格中的文本值更改单元格的颜色。
红色(0-500)
黄色(-5-0)
绿色(-350--5)
解决方案
看看。您可能甚至不需要VBA来执行此操作。
就是说,VBA代码看起来像这样:
Public Sub colorit()
Dim colRange As Range
Dim rowNum As Integer
Dim rnum As Integer
rnum = 20
设置colRange =范围(单元格(2,9),单元格(rnum,9))
对于rowNum = 1到colRange.Rows.Count
如果colRange。单元格(rowNum,1).Value< = -5 Then
colRange.Cells(rowNum,1).Interior.Color = RGB(0,255,0)
ElseIf colRange.Cells(rowNum, 1).Value< = 0然后
colRange.Cells(rowNum,1).Interior.Color = RGB(255,255,0)
ElseIf colRange.Cells(rowNum,1).Value< ; = 500然后
colRange.Cells(rowNum,1).Interior.Color = RGB(255,0,0)
End If
Next rowNum
End Sub
Using a macro I have consolidated info from several workbooks into one sheet in new workbook.
In one column I have created a named range called ColRange. That column has numbers ranging from -350 to 500.
How do I change the color of the cells based on the value of the text in the cell.
red(0-500)yellow(-5-0)green(-350--5)
解决方案
Have a look at conditional formatting. You may not even need VBA to do this.
That being said, the VBA code would look something like this:
Public Sub colorit()
Dim colRange As Range
Dim rowNum As Integer
Dim rnum As Integer
rnum = 20
Set colRange = Range(Cells(2, 9), Cells(rnum, 9))
For rowNum = 1 To colRange.Rows.Count
If colRange.Cells(rowNum, 1).Value <= -5 Then
colRange.Cells(rowNum, 1).Interior.Color = RGB(0, 255, 0)
ElseIf colRange.Cells(rowNum, 1).Value <= 0 Then
colRange.Cells(rowNum, 1).Interior.Color = RGB(255, 255, 0)
ElseIf colRange.Cells(rowNum, 1).Value <= 500 Then
colRange.Cells(rowNum, 1).Interior.Color = RGB(255, 0, 0)
End If
Next rowNum
End Sub
这篇关于基于细胞中的价值的彩色细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!