简体   繁体   中英

Do until i = the number of numbered cells in a specific sheet

I'm trying to execute a macro several times. I want to execute it based on the number of cells containing a number within a specific column on a specific sheet.

I tried to write a do until loop, but I get a syntax error for the "do until i =" line.

Sub copy_paste_to_first_blank_row()

Dim i As Integer

i = 1

Do Until i = COUNT(input!E:E)

    Range("A1:D76").Select
    Selection.Copy
    Cells(Range("A1000000").End(xlUp).Row + 1, 1).Select
    ActiveSheet.Paste

i = i + 1

Loop

End Sub

Since in my given situation count(input!E:E) = 93, I would expect the loop to be run 92 times and then stop. I get the error message "compile error: syntax error" when I try to run it; it highlights the "do until" line.

...

Now I've tried

Dim i As Integer, x As Integer

Worksheets("input").Activate
x = Range("E:E").Count

i = 1

Do Until i = x

    Range("A1:D76").Select
    Selection.Copy
    Cells(Range("A1000000").End(xlUp).Row + 1, 1).Select
    ActiveSheet.Paste

i = i + 1

Loop

I get the error "Run-time error '6': Overflow".

Integer is a 16-bit signed integer type, so the maximum value it can take is 32,767. Anything greater than that will raise an Overflow run-time error.

Fix it by declaring your row numbers as Long , a 32-bit signed integer type, with a maximum value well beyond the number of rows on an Excel worksheet.

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