vba do while循环不会退出循环

时间:2016-04-06 01:45:41

标签: excel excel-vba loops vba

我一直在试图弄清楚如何摆脱do while循环。我的想法是复制找到我要找的东西的第一个单元格并将其粘贴到a1中以将其用作打破循环的参考......但它无效。

    Range("A1").Select
    Cells.Find(What:="pdl", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate
    Dim x As String
    ActiveCell.Select
    Selection.Copy
    Range("A1").Select
    ActiveSheet.Paste
    Cells.FindNext(After:=ActiveCell).Activate

    Do While True
        ActiveCell.Select
        If ActiveCell.Address = Range("a1") Then
            Exit Do
        Else
            Selection.Copy
            ActiveCell.Offset(0, 1).Select
            ActiveSheet.Paste
            Cells.FindNext(After:=ActiveCell).Activate
        End If
    Loop

End Sub

它只是循环通过退出do。

1 个答案:

答案 0 :(得分:0)

    If ActiveCell.Address = Range("a1") Then
        Exit Do
    Else

Activecell.Address永远不会= Range(" a1")(好吧它可以,但你需要单元格A1中的文字读取" $ A $ 1"

如果要测试Activecell是否为单元格A1,则可以执行以下操作:

    If ActiveCell.Address = "$A$1" Then
        Exit Do
    Else

或者这个

    If ActiveCell.Address = Range("A1").Address Then
        Exit Do
    Else

但是我鼓励你做一些关于如何在不使用select和selection的情况下构建宏的研究,因为在运行时使用它们的时间非常昂贵。

相关问题