VBA 学习 :编译错误 else 没有 if 您所在的位置:网站首页 else没有前面的if是什么意思 VBA 学习 :编译错误 else 没有 if

VBA 学习 :编译错误 else 没有 if

2023-12-27 17:42| 来源: 网络整理| 查看: 265

'二层嵌套 1KG=0.45榜 Sub changeUnit() Dim i, j i = 2 If Cells(15, 8) = "千克" Then Do While Cells(i, 1) "" j = 2 Do While Cells(i, j) "" Cells(i, j) = Cells(i, j) * 0.45 j = j + 1 Loop i = i + 1 Loop Cells(15, 8) = "英镑" ActiveSheet.Shapes.Range(Array("Button 1")).Select Selection.Characters.Text = "转换为" & Cells(15, 8) With Selection.Characters(Start:=1, Length:=5).Font .Name = "等线" .FontStyle = "常规" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = 1 ElseIf Cells(15, 8) = "英镑" Then Do While Cells(i, 1) "" j = 2 Do While Cells(i, j) "" Cells(i, j) = Cells(i, j) / 0.45 j = j + 1 Loop i = i + 1 Loop Cells(15, 8) = "千克" ActiveSheet.Shapes.Range(Array("Button 1")).Select Selection.Characters.Text = "转换为" & Cells(15, 8) With Selection.Characters(Start:=1, Length:=5).Font .Name = "等线" .FontStyle = "常规" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = 1 End If End Sub

上述代码执行过程中出现如题所示的错误,通过检查发现

if 条件 then

elseif 条件 then

end if

上述分支语句并没有问题。那么问题出在哪里呢?

下面提供正确的答案:

'二层嵌套 1KG=0.45榜 Sub changeUnit() Dim i, j i = 2 If Cells(15, 8) = "千克" Then Do While Cells(i, 1) "" j = 2 Do While Cells(i, j) "" Cells(i, j) = Cells(i, j) * 0.45 j = j + 1 Loop i = i + 1 Loop Cells(15, 8) = "英镑" ActiveSheet.Shapes.Range(Array("Button 1")).Select Selection.Characters.Text = "转换为" & Cells(15, 8) With Selection.Characters(Start:=1, Length:=5).Font .Name = "等线" .FontStyle = "常规" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = 1 End With ElseIf Cells(15, 8) = "英镑" Then Do While Cells(i, 1) "" j = 2 Do While Cells(i, j) "" Cells(i, j) = Cells(i, j) / 0.45 j = j + 1 Loop i = i + 1 Loop Cells(15, 8) = "千克" ActiveSheet.Shapes.Range(Array("Button 1")).Select Selection.Characters.Text = "转换为" & Cells(15, 8) With Selection.Characters(Start:=1, Length:=5).Font .Name = "等线" .FontStyle = "常规" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = 1 End With End If End Sub

是的 ,with 语句要进行end



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有