Excel 如何获取以某种颜色高亮显示的单元格值
有没有人能帮我得到用某种颜色突出显示的单元格值。 下面是我可以得到地址的代码。但当我更改为.cell.Value时,它会弹出对象引用错误Excel 如何获取以某种颜色高亮显示的单元格值,excel,vba,Excel,Vba,有没有人能帮我得到用某种颜色突出显示的单元格值。 下面是我可以得到地址的代码。但当我更改为.cell.Value时,它会弹出对象引用错误 Sub ColorCellValue() Dim rCell As Range Dim lColor As Long Dim rColored As Range Dim rs As String Dim rng1 As Range 'Dim EmailRange As Range 'Select the
Sub ColorCellValue()
Dim rCell As Range
Dim lColor As Long
Dim rColored As Range
Dim rs As String
Dim rng1 As Range
'Dim EmailRange As Range
'Select the color by name (8 possible)
'vbBlack, vbBlue, vbGreen, vbCyan,
'vbRed, vbMagenta, vbYellow, vbWhite
'lColor = vbGreen
'If you prefer, you can use the RGB function
'to specify a color
lColor = RGB(0, 255, 0)
Set rng1 = ThisWorkbook.Worksheets("Sheet1").Range("B:B")
Set rColored = Nothing
For Each rCell In rng1
If rCell.Interior.color = RGB(0, 255, 0) Then
If rColored Is Nothing Then
Set rColored = rCell
Else
Set rColored = Union(rColored, rCell)
End If
End If
Next
If rColored Is Nothing Then
MsgBox "No cells match the color"
Else
rColored.Select
MsgBox "Selected cells match the color:" & _
vbCrLf & rColored.Address
End If
Set rCell = Nothing
Set rColored = Nothing
End Sub
```
请找到下面的工作和测试代码。它需要B列中的一些绿色RGB(0255,0)=65280-单元格和绿色单元格中的一些条目。请注意,它在搜索中省略了单元格B1
Sub ColorCellValue()
Dim Rng1 As Range
Dim rCell As Range
Dim lColor As Long
Dim rColored As Range
Dim Txt As String
Dim i As Long
With ThisWorkbook.Worksheets("Sheet1")
Set Rng1 = .Range(.Cells(2, 2), .Cells(.Rows.Count, "B").End(xlUp))
End With
lColor = 65280
'If you prefer, you can use the RGB function to specify a color
lColor = RGB(0, 255, 0)
For Each rCell In Rng1
If rCell.Interior.Color = lColor Then
If rColored Is Nothing Then
Set rColored = rCell
Else
Set rColored = Union(rColored, rCell)
End If
End If
Next
If rColored Is Nothing Then
Txt = "No cells match the color."
Else
Txt = "Selected cells match the color:"
For i = 1 To rColored.Areas.Count
Txt = Txt & vbCr & rColored.Areas(i).Address _
& " = " & rColored.Areas(i).Cells(1).Value
Next i
rColored.Select
End If
MsgBox Txt, vbInformation, "Search report"
End Sub
您可以调试.Print
rColored.Address
,但如果范围包含多个单元格,则无法调试.PrintrColored.Value
。由于范围不是连续的,因此您可能无法通过它循环查看每个单元格。您可能需要遍历其所有区域
,然后对区域(i)中的每个单元格执行。代码——本质上是你的代码,我只是稍微整理了一下——是可以的。它生成要连接的范围。因此,问题一定是您试图访问范围的方式。我的代码就是这样做的。因此,您可以复制我的方法以用于您自己的计划。您正在尝试的修订代码是什么?不确定您正在做什么,但您无法一次性访问多个单元格的值。您好,SJR,有没有其他可能获得单元格值的方法?您的代码对我有效,但不适用于Set rng1=ThisWorkbook.Worksheets(“Sheet1”).Range(“B:B”)
这是140万个单元格循环使用,用于。。。每个
。如果你限制范围,它就会起作用。我将范围设置为B2:B50,并获得MsgBox。然后我用rColored循环遍历单元格并打印每个单元格的值。嗨,你能告诉我如何获得每个单元格的值吗?但是当我给出如下数据时:padmini。chandrashekar@gmail.com西蜀。c@gmail.com阿比。d@gmail.com阿比。g@gmail.com,我只得到第一个值作为输出。请检查我的MsgBox是否只显示第一个单元格的值如果一个区域中有多个单元格,请比较&“=”&rColored.Areas(i).Cells(1).value
。第二个单元格将是r彩色的.Areas(i).Cells(2).Value
。您可以使用rColored.area(1).Cells.Count
Hi我尝试了rColored.Areas(I).Cells.Count,但它给出的是有色单元格的计数而不是值。我还尝试了rColored.Areas(i).Cells(i).value,它并没有给出所有的单元格值。它给出了第一个和最后一个答案,这是正确的。但是,您可以使用Count
构建一个循环,从中提取值。事实上,对于rColored.Area(1)中的每个单元格,单元格:Debug.Print Cell.Value:Next Cell
,其中Cell
是一个范围,也应该可以工作。感谢您的指导,我按照您的建议使用了下面的代码并获得了输出<代码>对于i=1到rColored.Areas.Count,对于rColored.Areas(i)中的每个单元格,单元格Txt=Cell.Value MsgBox Txt Next Cell Next i