VBA - 运行时间1004

问题描述 投票:-1回答:2

我正在尝试这个命令

Sub Name() 
Dim n As Integer
Do Until Cells(2, n) = "" 
    n = n + 1 
    Cells(2, n).Value = n 
Loop
End Sub

但我有“运行时错误1004:应用程序定义或对象定义的错误”消息。我在这里想念的是什么?

excel excel-vba
2个回答
0
投票

根据注释,您将永远循环,因为您的单元格将始终具有已分配的值,然后检查值。在指定当前单元格的值后移动n增量。然后,您的支票将是正确的。

Sub Name() 
    Dim n As Integer
    n=1 ' initialize N to where you want to start.
    Do Until Cells(2, n) = ""   
        Cells(2, n).Value = n 
        n = n + 1 //move this line to end of loop.
    Loop
End Sub

0
投票

也许子名称不正确,如果列n + 1 =“”也可以停止循环

编辑:我忘了尝试的另一件事是,do until cells(1,n)<>=""

Sub nn()
    Dim n
    n = 1
    Do Until Cells(2, n + 1) = ""
        n = n + 1
        Cells(2, n).Value = n
    Loop

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.