简体   繁体   中英

Excel VBA run-time error 1004 which I can't identify

This is my code:

Sub VerticalLoop()

Dim i As Integer

Range("VerticalLoop").Activate

i = 1

Do While i <= Range("VerticalLoop").Rows.Count

ActiveCell.Value = i
ActiveCell.Offset(1, 0).Activate
i = i + 1

  Loop



 End Sub

For some reason I can't run this. When I press F8, the error pops up when I reach the line "i=1", so I don't know exactly what I did wrong.

如果未首先激活包含“已定义名称”的图纸,则此代码将失败!

The error is in the line

Range("VerticalLoop").Activate

The error is probably a typo in the name of the range. Are you sure it isn't supposed to be "Vertical_Loop" (with the underscore)?

On the other hand, Gary's Student is correct that a '1004' error could be triggered by trying to activate a range in an inactive sheet. The error message itself (and not just the error number) can tell you more about the exact source of the error. Alternatively, you could break that line into two lines like thus:

Dim VLoop As Range
Set VLoop = Range("VericalLoop")
VLoop.Activate

If the Set VLoop line throws the error then the problem is with your name. If the second line throws the error then then problem is with what is active.

As an added benefit -- once you debug this error you now have a useful range variable allowing you to use expressions like VLoop.Rows.Count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM