当前位置: 代码迷 >> VBA >> VBA怎么快速定位到某个单元格,求大神指点迷津!
  详细解决方案

VBA怎么快速定位到某个单元格,求大神指点迷津!

热度:3464   发布时间:2013-02-26 00:00:00.0
VBA如何快速定位到某个单元格,求大神指点迷津!!!

如图,我试图找出最后一个"A"的行号,最后一个"B"的行号,最后一个"C"的行号,由于"A"的数量太庞大,于是我写了如下代码:

Sub ABC()
    Dim i, RowSum, A_LastNum, B_LastNum, C_LastNum As Long
    RowSum = Range("A1", Range("A1").End(xlDown)).Cells.Count
    i = RowSum + 1
    Do
        i = i - 1
        If (Cells(i, 1).Value = "C") And (Cells(i + 1, 1).Value <> "C") Then
            C_LastNum = i
        Else
            If (Cells(i, 1).Value = "B") And (Cells(i + 1, 1).Value <> "B") Then
                B_LastNum = i
            Else
                If (Cells(i, 1).Value = "A") And (Cells(i + 1, 1).Value <> "C") Then
                    A_LastNum = i
                    Exit Do
                End If
            End If
        End If
    Loop
End Sub

这段代码从这一列的最后开始循环,但是如果"B","C"的数量也比较多的话,循环会花费很多时间,我想问各位大神,有没有什么能快速定位到最后一个"A",最后一个"B",最后一个"C"而避免循环的方法?求大神指点迷津啊!!!

------解决方案--------------------------------------------------------
Sub ABC()

' 这是一个宏
   Dim i, RowSum, A_LastNum, B_LastNum, C_LastNum As Long
    ActiveSheet.Cells.Find(What:="B").Activate
    A_LastNum = Selection.Row - 1
    ActiveSheet.Cells.Find(What:="C").Activate
    B_LastNum = Selection.Row - 1
    For i = Selection.Row To 65534
        If ActiveSheet.Cells(i, 1).Value = "C" And ActiveSheet.Cells(i + 1, 1).Value <> "C" Then
            C_LastNum = i
            Exit For
        End If
    Next i
    Debug.Print A_LastNum, B_LastNum, C_LastNum
End Sub

------解决方案--------------------------------------------------------
引用:
如图,我试图找出最后一个"A"的行号,最后一个"B"的行号,最后一个"C"的行号,由于"A"的数量太庞大,于是我写了如下代码:
Visual Basic code?1234567891011121314151617181920Sub ABC()    Dim i, RowSum, A_LastNum, B_LastNum, C_LastNum As Long    RowSum = Range(……