Excel VBA,比较多个列值,颜色单元格

我是Excel中的VBA新手,遇到了一些麻烦。 我有3列A,C和F.我只想突出显示那些列中的单元格,如果他们匹配2个条件之一。 在列A中突出显示具有重复值的单元格,然后仅在C列和F列的值为99.99或更大的情况下突出显示列C和F中的单元格,并且列F在单元格中具有除“testing”之外的任何内容。

Sub Highlight() Dim index As Integer For index = 1 To 4 'Checks if any cells in Column C has value greater than 99.99 when Column F isn't "Test" or checks if multiple values exist in Column A (which I don't know) If Range("C1") And Cell.Value > "99.99" And Range("F1") And Cell.Text <> "Current" Then 'Highlighs both cell values Yellow (this is where I run into trouble) Cell.Interior.ColorIndex = vbYellow End If Next index End Sub 

你只是指你的范围错误。 Cell没有设置。 我看不到你在哪里提到A列。

此外,如果您使用内置的颜色常量,则应使用.Color而不是ColorIndex。

 Sub Highlight() Dim index As Integer Dim ws As Worksheet 'set the sheet to use Set ws = Sheet1 For index = 1 To 4 'Checks if any cells in Column C has value greater than 99.99 when Column F isn't "Test" or checks if multiple values exist in Column A (which I don't know) If ws.Range("C" & index).Value > "99.99" And ws.Range("F" & index).Text <> "Current" Then 'Highlighs both cell values Yellow (this is where I run into trouble) ws.Range("C" & index).Interior.Color = vbYellow ws.Range("F" & index).Interior.Color = vbYellow End If Next index End Sub 

在旁边注意。 你可能会更愿意使用条件格式来实现你想要的而不是VBA。

在互联网上有很多的教程:

http://chandoo.org/wp/2009/03/13/excel-conditional-formatting-basics/

http://spreadsheets.about.com/od/advancedexcel/tp/090822-excel-conditional-formatting-hub.htm