我有一张看起来像这样的表:
我想要一个VBA代码来查找客户编号和"衡量"该块的大小有多大。
例:
8887有两列
8736有两列。
8602有一栏。
我使用range.find来查找客户编号,然后从列中循环一个偏移量以找到"阻止"结束。
当块不是相同的内部颜色,另一个客户写入单元格或找到单元格边框时,块结束。
Set C = rng.Find(Search, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not C Is Nothing Then
FirstAddress = C.Address
FirstColor = C.Interior.Color
Do
' If it's just one cell width (column CJ)
If C.Borders(xlEdgeRight).LineStyle <> xlNone Then
Platser = Platser & Mid(C.Address, 2) & "," 'append the locations string
Else
Platser = Platser & Mid(C.Address, 2) & "," 'append the locations string
i = 1
' This is the loop that is causing problem.
'As I see it it should stop at i=1 but it keeps looping
While C.Offset(0, i).Interior.Color = FirstColor And _
C.Offset(0, i).Value = "" And _
C.Offset(0, i).Borders(xlEdgeRight).LineStyle <> xlNone
'append the locations string
Platser = Platser & Mid(C.Offset(0, i).Address, 2) & ","
i = i + 1
Wend
End If
' here I find the next cell with the same value,
' but this has nothing to do with the problem.
Set C = rng.Find(Search, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
after:=C, _
MatchCase:=False)
Loop While Not C Is Nothing And FirstAddress <> C.Address
在此代码之后,单元格地址以获取第3行上的数字 上面代码的输出是:
364,363,362,361,360,359,358,357,356,354,354
因此它找到了值8736,但它没有看到应该使其停止循环的单元格边框。
什么可能导致这种行为?
答案 0 :(得分:1)
While C.Offset(0, i).Interior.Color = FirstColor And _
C.Offset(0, i).Value = "" And _
C.Offset(0, i).Borders(xlEdgeRight).LineStyle <> xlNone
应该是
While C.Offset(0, i).Interior.Color = FirstColor And _
C.Offset(0, i).Value = "" And _
C.Offset(0, i).Borders(xlEdgeRight).LineStyle = xlNone