代码之家  ›  专栏  ›  技术社区  ›  Andreas

单元格边框未“找到”

  •  0
  • Andreas  · 技术社区  · 8 年前

    我有一张这样的纸:

    enter image description here

    我想要一个VBA代码来找到客户编号,并“测量”它所在的块有多大。

    例子:
    8887有两列。
    8736有两列。
    8602有一列。


    当内部颜色不同、单元格中写入另一个客户或找到单元格边框时,块结束。

    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
    

    在这个代码i之后,输入单元格地址以获得第3行的数字。
    上述代码的输出为:

    所以它找到了值8736,但没有看到应该使其停止循环的单元格边界。
    什么会导致这种行为?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Tim Williams    8 年前
    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
    
    推荐文章