简体   繁体   中英

VBA expected end of statement

I am trying to edit my excel table with VBA but an error appears while compiling. It doesnt recognize line 2 and line 10.

Sub IfThenElse()
    Dim i As Integer = 23
    While Not IsNull(Cells(i, 35).Value)
        If Cells(i, 35).Value > 1E+16 Then
            Cells(i, 4).Value = Cells(i, 35).Value / 10
        Else
            Cells(i, 4).Value = Cells(i, 35).Value
            i = i + 1
        End If
    End While
End Sub
  1. You cannot declare a variable and set a value at the same time Dim i As Integer = 23

  2. Row counts are of type Long not Integer , Excel has more rows than Integer can handle.

     Dim i As Long i = 23
  3. WhileEnd While is no valid syntax, you need to use Do WhileLoop (see Do...Loop statement ).

  4. It is very unlikely that a cell value is Null if you are looking for an empty cell use IsEmpty or check for vbNullString

     Do While Not IsEmpty(Cells(i, 35).Value) 'or Do While Not Cells(i, 35).Value = vbNullString If Cells(i, 35).Value > 1E+16 Then Cells(i, 4).Value = Cells(i, 35).Value / 10 Else Cells(i, 4).Value = Cells(i, 35).Value i = i + 1 End If Loop
  5. Not sure what exactly you are doing but i = i + 1 might need to come after End If .

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