【问题标题】:Userform throwing error when checkbox = true in If Then Else Statement当 If Then Else 语句中的复选框 = true 时,用户窗体抛出错误
【发布时间】:2020-09-26 15:06:23
【问题描述】:

为以下代码引发错误。错误代码显示“编译错误:预期:然后或转到”

这里的代码有问题。

Private Sub Userform.Initialize()
    If Worksheets("Sheet1").Range("P1").Value = "Y"
            Then
            CheckBox1.Value = True
            Else
            CheckBox1.Value = False
    End If
End Sub

关于如何纠正此错误的任何见解?我觉得代码很可靠。

【问题讨论】:

  • Then 应该与 If 语句在同一行。如果要放到下一行,需要在行尾加上If语句的续行符(_)
  • 谢谢!当然,现在我觉得有点傻。大声笑。

标签: excel vba if-statement userform


【解决方案1】:

我想通了,我觉得有点愚蠢。我在初始 IF 语句末尾缺少下划线。

现在的代码如下:

Private Sub Userform.Initialize()
    If Worksheets("Sheet1").Range("P1").Value = "Y" _
        Then
        CheckBox1.Value = True
        Else
        CheckBox1.Value = False
    End If
End Sub

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 2015-01-05
    • 2015-11-18
    • 1970-01-01
    • 2013-06-18
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多