gpt4 book ai didi

excel - 循环遍历填充颜色的单元格直到空白

转载 作者:行者123 更新时间:2023-12-04 21:30:45 25 4
gpt4 key购买 nike

我正在尝试创建一个填充单元格的 Excel 文档(相关的单元格数量不同,有些只有 1 个其他 10+,列是相同的数字)

我想选择“activeCell 区域”。所以例如如果事件单元格是 A11,则选择从 A11 一直到 E14 的填充区域(所有蓝色单元格)。

这是我目前得到的,我认为我需要一个 while 循环,但我无法让它工作:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("N5:N1000")) Is Nothing Then
If Cells(Target.Row, 1).Interior.ColorIndex <> xlNone Then
If Cells(Target.Row, 14) = "x" Or Cells(Target.Row, 14) = "X" Then
Range("A" & ActiveCell.Row).Select

End If
End If
End If

End Sub

电子表格:
Excel sheet

步骤1:
enter image description here

第2步:
enter image description here

第 3 步:
enter image description here

最佳答案

如果要扩展单个单元格范围以覆盖相同填充的矩形范围,可以执行以下操作:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range

Set c = Application.Intersect(Target.Cells(1), Me.Range("N5:N1000"))

If Not c Is Nothing Then
If Me.Cells(c.Row, 1).Interior.ColorIndex <> xlNone And _
UCase(Me.Cells(Target.Row, 14)) = "X" Then

GetColorBlock(Me.Cells(c.Row, 1)).Select

End If
End If

End Sub

'Expand a single cell range to all neighboring cells with the same fill color
' (assumes colored range is rectangular)
Function GetColorBlock(c As Range) As Range
Dim tl As Range, br As Range, clr As Long
clr = c.Interior.Color
Set tl = c
Set br = c
Do While tl.Row > 1
If tl.Offset(-1, 0).Interior.Color <> clr Then Exit Do
Set tl = tl.Offset(-1, 0)
Loop
Do While tl.Column > 1
If tl.Offset(0, -1).Interior.Color <> clr Then Exit Do
Set tl = tl.Offset(0, -1)
Loop
Do While br.Row < Rows.Count
If br.Offset(1, 0).Interior.Color <> clr Then Exit Do
Set br = br.Offset(1, 0)
Loop
Do While br.Column < Columns.Count
If br.Offset(0, 1).Interior.Color <> clr Then Exit Do
Set br = br.Offset(0, 1)
Loop
Set GetColorBlock = c.Worksheet.Range(tl, br)
End Function

关于excel - 循环遍历填充颜色的单元格直到空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52733444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com