我有一个与我的SQL数据库集成的excel文件。打开时,excel文件从数据库中获取数据并粘贴到我的excel文件中。我编写了这个简单的代码,只要一个单元改变其值,就会改变单元格颜色:
Private Sub Worksheet_Change(ByVal Target As Range)
Target.Interior.ColorIndex = 6
End Sub
现在我想创建一个按钮来将数据更新到我的数据库中,但只创建更改其值的单元格。
有没有办法找到不同背景的细胞?如果不是,是否有其他方法可以跟踪改变其值的单元格?
答案 0 :(得分:1)
首先使用xldown和xltoright
查找数据范围然后运行for循环以检查每个单元格
在forloop内部使用if条件来检查单元格颜色
如果单元格颜色条件满足,则运行u want else endif的操作并使用for循环转到下一个单元格。
答案 1 :(得分:0)
以下是查找具有一种颜色的单元格的示例
根据需要进行编辑
'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)
搜索黄色单元格的示例
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