【问题标题】:VBA expected end of statementVBA 预期语句结束
【发布时间】:2022-01-22 23:14:51
【问题描述】:

我正在尝试使用 VBA 编辑我的 excel 表,但编译时出现错误。它不识别第 2 行和第 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

【问题讨论】:

标签: excel vba compiler-errors


【解决方案1】:
  1. 不能同时声明变量和设置值Dim i As Integer = 23

  2. 行数的类型为 Long 而不是 Integer,Excel 的行数超出了 Integer 的处理能力。

    Dim i As Long 
    i = 23
    
  3. WhileEnd While 是无效语法,您需要使用 Do WhileLoop(请参阅Do...Loop statement)。

  4. 如果您要查找空单元格,则单元格值不太可能是 Null,请使用 IsEmpty 或检查 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. 不确定您到底在做什么,但i = i + 1 可能需要在End If 之后。

【讨论】:

  • while 的 VBA 语法是 while/wend。然而,不赞成使用这种结构,而赞成使用更灵活的 do 循环结构。此外,在阅读 VBA 时,请确保您没有错误地进入 Visual Basic(VB 网络)页面
  • @freeflow 这就是为什么我建议使用Do 循环。
  • @peh 你解释的方式不是很清楚恕我直言
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多