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

时间:2016-01-29 21:11:06

标签: excel macos vba excel-vba-mac

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

如果当前价格高于先前价格,则关注价格列并设置颜色(黑色),如果当前价格低于先前价格,则设置另一种颜色(红色)。

另外,如何指定不是颜色常数的颜色?

代码段:

'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

干杯!!

1 个答案:

答案 0 :(得分:0)

我没有Mac,所以无法发出真实的答案,但如果你遇到困难,我只是想试着帮助我做你想做的事情:

  1. 有条件格式化&#34;&#34;在Excel中,您可以编写简单条件或更复杂的使用公式的条件(这些条件可以调用VBA函数)。通过这些,然后通过GUI完成选择格式。对我来说,这是进行条件格式化(而不是VBA)的明显而简单的方法。
  2. 对于自定义颜色,您必须通过指定自定义调色板来执行此操作,例如参见How to change the color palette for workbooks in Excel
  3. 希望这有帮助,

    TEPP