我对这一切都很新,所以如果我是愚蠢的,我现在会道歉。 我在使下面的代码正常运行时遇到了一些问题。 基本上,我要求它查看一列;
当我运行以下代码时,它会将所有单元格变为红色,是否有人能够解释我错过的内容?
Sub Format_Cells()
Dim arq As Range
Dim msl As Range
Dim Cell
Set arq = Range("B3:B500")
Set msl = Range("C3:C500")
For Each Cell In arq
'If Cell is blank then Cell Colour = White
If Cell.Value = "" Then
Cell.Interior.ColorIndex = 2
End If
'If Requisition Quantity is equal to Max Stock Level then Cell Colour = White
If arq(B3) = msl(C3) Then
Cell.Interior.ColorIndex = 2
End If
'If Requisition Quantity is less than Max Stock Level then Cell Colour = Green
If arq(B3) < msl(C3) Then
Cell.Interior.ColorIndex = 43
End If
'If Requisition Quantity is more than Max Stock Level then Cell Colour = Red
If arq(B3) > msl(C3) Then
Cell.Interior.ColorIndex = 46
End If
Next
MsgBox "The macro has finished running.", vbInformation
End Sub
答案 0 :(得分:0)
如上所述,CF可能是要走的路,但是你现有的代码作为起点,我刚修改为使用Offset(右边的1列)。你的四个条款是否全部相互排斥?
Sub Format_Cells()
Dim arq As Range
Dim msl As Range
Dim Cell As Range
Set arq = Range("B3:B500")
Set msl = Range("C3:C500")
For Each Cell In arq
'If Cell is blank then Cell Colour = White
If Cell.Value = vbnullstring Then
Cell.Interior.ColorIndex = 2
End If
'If Requisition Quantity is equal to Max Stock Level then Cell Colour = White
If Cell.Value = Cell.Offset(, 1).Value Then
Cell.Interior.ColorIndex = 2
End If
'If Requisition Quantity is less than Max Stock Level then Cell Colour = Green
If Cell.Value < Cell.Offset(, 1).Value Then
Cell.Interior.ColorIndex = 43
End If
'If Requisition Quantity is more than Max Stock Level then Cell Colour = Red
If Cell.Value > Cell.Offset(, 1).Value Then
Cell.Interior.ColorIndex = 46
End If
Next
MsgBox "The macro has finished running.", vbInformation
End Sub