2016-01-29 41 views
0

我正在尋找代碼語法來設置單元格顏色填充不透明度(更亮或更暗),具體取決於是否滿足條件。以下代碼將轉到最後一行數據,並朝向第一行。有條件地格式化單元格屬性 - 填充顏色和不透明度

它專注於價格欄,如果當前價格高於前一價格,則設置顏色(黑色),如果當前價格低於之前價格,則設置另一種顏色(紅色)。

另外,如何指定不是顏色常量的顏色?

代碼片段:

'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 

乾杯!

回答

0

我沒有在Mac所以不能發佈一個真正的答案,但如果你被卡住我只是想盡力幫忙做什麼,我會在你的鞋子做:

  1. 有這樣的在Excel中作爲「條件格式」,使您可以編寫簡單的條件或使用公式的更復雜的條件(這些條件可以調用VBA函數)。通過這些,選擇格式然後通過GUI完成。對我而言,這將是執行條件格式(而不是VBA)的顯而易見的方法。
  2. 爲自定義顏色,您需要做的是通過指定自定義調色板,例如參見How to change the color palette for workbooks in Excel

希望這有助於

特普

+0

感謝您的幫助,但我需要一個Excel Mac VBA解決方案。 – phreshsprout

相關問題