爲標題說我正在尋找一種方法來改變細胞的顏色的基礎上一定範圍內,如果它們比閾值標準較低或較高。因爲我想重新着色每一個閾值改變時發生,這是到目前爲止我的代碼,但我不斷收到就行溢出異常,我定義了基於RGB顏色代碼值EXCEL VBA改變背景顏色 - RGB不起作用
Option Explicit ' Force explicit variable declaration
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim thresholdValue As Double
Dim resetColumnStart As Integer
Dim resetRowStart As Integer
Dim resetRowEnd As Integer
Dim ii, kk As Integer
Dim lightColor As Integer
Dim darkColor As Integer
Dim cellInRange As Excel.Range
thresholdValue = Sheets("ThresholdValues").Range(Cells(Target.Row, Target.Column), Cells(Target.Row, Target.Column))
If Target.Column = 2 Then
If Target.Row = 3 Then
resetColumnStart = 11
lightColor = RGB(242, 221, 220)
darkColor = RGB(217, 151, 149)
ElseIf Target.Row = 4 Then
resetColumnStart = 18
lightColor = RGB(219, 229, 241)
darkColor = RGB(149, 179, 215)
Else
Exit Sub
End If
Else
Exit Sub
End If
resetRowStart = 3
For ii = 2 To 4 Step 1
Sheets(ii).Activate
resetRowEnd = Range("A65536").End(xlUp).Row
For kk = 1 To 7 Step 2
'reset the background colour for each data sheet
With Range(Cells(resetRowStart, resetColumnStart + kk), Cells(resetRowEnd, kk))
.Interior.Color = lightColor
.Font.Color = RGB(0, 0, 0)
End With
With Range(Cells(resetRowStart, kk + 1), Cells(resetRowEnd, kk + 1))
.Interior.Color = darkColor
.Font.Color = RGB(255, 255, 255)
End With
Next kk
Next ii
'color the values that are below the threshold for each data sheet
For ii = 2 To 4 Step 1
Sheets(ii).Activate
resetRowEnd = Range("A65536").End(xlUp).Row
currentRange = Range(Cells(resetRowStart, resetColumnStart), Cells(resetRowEnd, resetColumnStart + 6))
For Each cellInRange In currentRange
If cellInRange.Value < thresholdValue Then
cellInRange.Interior.Color = RGB(0, 255, 255)
cellInRange.Font.Color = RGB(255, 0, 0)
End If
Next cellInRange
Next ii
End Sub
我完全地新的VBA編碼,所以它似乎我缺少的東西essentialy,我希望你可以幫助我,因爲編碼這應該很容易,我失去了這麼多時間花哨的東西,而沒有做實際工作
你有沒有考慮條件格式? https://www.youtube.com/watch?v=9Dk1JoL_Pe4 – dav1dsm1th 2014-12-03 13:19:27
的問題是,我產生了Excel與MATLAB腳本文件,所以我想這不會是可能的 – Ingrid 2014-12-03 13:24:21