简体   繁体   中英

excel vba compile error sub or function not defined

I'm running a simple for loop but I get en error:

compile error: sub or function not defined

I made a module in vba:

Option Explicit

Sub 100group()

Dim i As Integer, E As Integer
  E = 101
  For i = 2 To 37317
    If (cell(i, 1) < E) Then cell(i, 3) = E Else E = E + 100
    End If

  Next i

End Sub
  • cell(i, 1) should be Cells(i, 1)

  • Also, sub/function.variable names cannot start with a number.

  • Also also, Your If statement syntax is well out.

  • Finally you will hit a further error because i is declared as an Integer which can only hold a value of up to 32,768. So you need:

     Dim i As Long

As a Long data type can hold a value of up to 2,147,483,647


To summarise, you should be using:

Option Explicit

Sub group100()

Dim i As Long, E As Long
  E = 101
  For i = 2 To 37317
      If (Cells(i, 1) < E) Then
          Cells(i, 3) = E 
      Else 
          E = E + 100
      End If
  Next i

End Sub

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