条件格式单元格属性 - 填充颜色和不透明度



我正在寻找根据满足或不满足的条件设置单元格颜色填充不透明度(浅色或深色)的代码语法。以下代码转到数据的最后一行,并向上处理第一行。

它专注于价格列,如果当前价格

大于前一个价格,则设置颜色(黑色),如果当前价格低于前一个价格,则设置另一种颜色(红色)。

此外,如何指定不是颜色常量的颜色?

代码片段:

'Loop through a recordset from the bottom up and determine
'if current row cell value is greater than the previous one
'recordset sort is descending date (most current date at top)
'go to oldest date and work up until most current date
Public Sub testing()
Dim ws As Worksheet
Dim endRow As Integer
Dim i As Integer
Dim thisRow As Integer
Dim nextRow As Integer
Dim pastRow As Integer

Set ws = ThisWorkbook.Worksheets("DataSheet")
endRow = ws.Range("A" & Rows.Count).End(xlUp).Row 'go to last row and work up
i = endRow
While i >= 1
'Working up the sheet doing whatever
        thisRow = i          'the current row or date under consideration
        nextRow = i - 1      'the row above (next day) - working up
        pastRow = i + 1      'the row below (previous day)
If pastRow < endRow Then     'skip the last row of data, start at 2nd to last
    If nextRow > 0 Then 'as long as there is a tomorrow lets work with today
        'If current day´s value is greater than previous day, 
        'color current day cell (black), else color (Blue)
        If Cells(i, 2).Value > Cells(pastRow, 2).Value Then
            Range(Cells(i, 8), Cells(i, 8)).Interior.Color _
                 = ColorConstants.vbBlack
            'looking for code to set color intensity (lighter or darker)
            'also how to define a color other than a color constant
        Else
            Range(Cells(i, 8), Cells(i, 8)).Interior.Color _ 
                 = ColorConstants.vbRed
        End If
    End If
End If
i = i - 1
Wend
End Sub

干杯!!

我没有

Mac,所以无法发布真正的答案,但如果你被困住了,我只是想试着帮忙解决我会在你的鞋子里做什么:

  1. Excel中存在"条件格式"之类的东西,它使您能够编写使用公式的简单条件或更复杂的条件(这些条件可以调用VBA函数)。有了这些,然后通过GUI选择格式。对我来说,这将是进行条件格式化(而不是 VBA)的明显而简单的方法。
  2. 至于自定义颜色,您必须通过指定自定义调色板来执行此操作,例如请参阅如何在Excel中更改工作簿的调色板

希望这有帮助,

特普

最新更新