在VBA中具有不同背景的单元格

我有一个与我的SQL数据库集成的Excel文件。 打开时,excel文件从数据库中获取数据并粘贴到我的excel文件中。 我已经写了这个简单的代码,当一个单元格改变它的值时,它改变单元格的颜色:

Private Sub Worksheet_Change(ByVal Target As Range) Target.Interior.ColorIndex = 6 End Sub 

现在我想创build一个button来将数据更新到我的数据库中,但只有更改其值的单元格。

有没有办法find不同背景的细胞? 如果否,有没有其他方法来跟踪改变其值的单元格?

首先使用xldown和xltoright查找数据的范围

然后运行一个for循环来检查每个单元格

如果条件检查单元格的颜色,在forloop内使用

如果单元格颜色条件满足,则运行u想要的其他endif动作,并使用for循环转到下一个单元格。

这里是find一种颜色的单元格的例子

编辑,因为你需要它

 'Select the color by name 'vbBlack, vbBlue, vbGreen, vbCyan, 'vbRed, vbMagenta, vbYellow, vbWhite 'or if you prefer, you can use the RGB function 'to specify a color 'Colr = RGB(0, 112, 192) 

示例search黄色单元格

 Sub Test() Dim Cel As Range Dim Colr As Long Dim Colred As Range Colr = vbYellow Set Colred = Nothing '// select cells to search or set range For Each Cel In Selection If Cel.Interior.Color = Colr Then If Colred Is Nothing Then Set Colred = Cel Else Set Colred = Union(Colred, Cel) End If End If Next If Colred Is Nothing Then MsgBox "No cells match that color" Else Colred.Select MsgBox "Selected cells match the color:" & _ vbCrLf & Colred.Address End If Set Cel = Nothing Set Colred = Nothing End Sub