vb6.0提示编译错误:else没有if,请问如何解决?

tianlancheak 2010-05-05 07:32:50
这段代码提示else没有if,请问如何解决?这段代码是当text7,text8,text9或text10,text11.text12没有输入数据时则除3,有数据时则除4.还有就是如何为text7,text8,text9或text10,text11.text12,当只输入其中一个数据,其他不输入时则提示:请输入数据!并不进行计算!
Private Sub Command1_Click()
Text16 = Val(Text1.Text) + Val(Text4.Text) + Val(Text7.Text) + Val(Text10.Text)
Text17 = Val(Text2.Text) + Val(Text5.Text) + Val(Text8.Text) + Val(Text11.Text)
Text18 = Val(Text3.Text) + Val(Text6.Text) + Val(Text9.Text) + Val(Text12.Text)
If Text7.Text = "" Then
Text8.Text = ""
Text9.Text = ""
Text16 = Val(Text16.Text) / 3
Text17 = Val(Text17.Text) / 3
Text18 = Val(Text18.Text) / 3
Else
Text10.Text = ""
Text11.Text = ""
Text12.Text = ""
Text16 = Val(Text16.Text) / 3
Text17 = Val(Text17.Text) / 3
Text18 = Val(Text18.Text) / 3
Else
Text16 = Val(Text16.Text) / 4
Text17 = Val(Text17.Text) / 4
Text18 = Val(Text18.Text) / 4
End If
End Sub


谢谢各位高手!
...全文
866 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
射天狼 2010-05-06
  • 打赏
  • 举报
回复
怎么出现两上没有条件的else?
如楼上提示,将中间的else加上if就可以了!
of123 2010-05-06
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim i As Integer, divisor As Integer
Dim v1 As Double, v2 As Double, v3 As Double


divisor = 4

'检查
v1 = Val(Text1(6))
v2 = Val(Text1(7))
v3 = Val(Text1(8))

If (v1 + v2 + v2 <> 0) And (v1 * v2 * v3) = 0 Then
For i = 6 To 8
If Text1(i) = "" Then
Text1(i).SetFocus
MsgBox "请输入数据!"
Exit Sub
End If
Next i
End If

If v1 + v2 + v3 = 0 Then divisor = divisor - 1

v1 = Val(Text1(9))
v2 = Val(Text1(10))
v3 = Val(Text1(11))

If (v1 + v2 + v2 <> 0) And (v1 * v2 * v3) = 0 Then
For i = 9 To 11
If Text1(i) = "" Then
Text1(i).SetFocus
MsgBox "请输入数据!"
Exit Sub
End If
Next i
End If

If v1 + v2 + v3 = 0 Then divisor = divisor - 1

v1 = 0
v2 = 0
v3 = 0
For i = 0 To 9 Step 3
v1 = v1 + Val(Text1(i))
v2 = v2 + Val(Text1(i + 1))
v3 = v3 + Val(Text1(i + 2))
Next i

Text1(15) = v1 / divisor
Text1(16) = v2 / divisor
Text1(17) = v3 / divisor
End Sub
threenewbee 2010-05-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 slowgrace 的回复:]
如果你像1楼那样写代码时养成正确缩进的习惯,就不会出这种错了 :-)
[/Quote]
西西知道编译原理里面讲的经典if二义性问题么?
比如
if (xxx)
if (yyy)
...
else
...

其实应该是
if (xxx)
if (yyy)
...
else
...

因为编译器可不管缩进 :)
在VB6里面是
If xxx Then If yyy Then ... Else ...
熊孩子开学喽 2010-05-05
  • 打赏
  • 举报
回复
if基本用法错误
不应该啊不应该
slowgrace 2010-05-05
  • 打赏
  • 举报
回复
如果你像1楼那样写代码时养成正确缩进的习惯,就不会出这种错了 :-)
贝隆 2010-05-05
  • 打赏
  • 举报
回复
多分支的话最好用Select Case语句,更有条理些。
threenewbee 2010-05-05
  • 打赏
  • 举报
回复
最基本的语法。

看书,If Else End If的概念。
clear_zero 2010-05-05
  • 打赏
  • 举报
回复
If Text7.Text = "" Then
Text8.Text = ""
Text9.Text = ""
Text16 = Val(Text16.Text) / 3
Text17 = Val(Text17.Text) / 3
Text18 = Val(Text18.Text) / 3
ElseIf XXXX Then 'XXXX为条件

Text10.Text = ""
Text11.Text = ""
Text12.Text = ""
Text16 = Val(Text16.Text) / 3
Text17 = Val(Text17.Text) / 3
Text18 = Val(Text18.Text) / 3
Else
Text16 = Val(Text16.Text) / 4
Text17 = Val(Text17.Text) / 4
Text18 = Val(Text18.Text) / 4
End If
End Sub
SYSSZ 2010-05-05
  • 打赏
  • 举报
回复
明显语法
If Text7.Text = "" Then
……

Else
……

Else
……
End if
是错的

luofenghen 2010-05-05
  • 打赏
  • 举报
回复

Private Sub Command1_Click()
Text16 = Val(Text1.Text) + Val(Text4.Text) + Val(Text7.Text) + Val(Text10.Text)
Text17 = Val(Text2.Text) + Val(Text5.Text) + Val(Text8.Text) + Val(Text11.Text)
Text18 = Val(Text3.Text) + Val(Text6.Text) + Val(Text9.Text) + Val(Text12.Text)
dim anything as string
select case anything
case 条件1
Text8.Text = ""
Text9.Text = ""
Text16 = Val(Text16.Text) / 3
Text17 = Val(Text17.Text) / 3
Text18 = Val(Text18.Text) / 3
case 条件2
Text10.Text = ""
Text11.Text = ""
Text12.Text = ""
Text16 = Val(Text16.Text) / 3
Text17 = Val(Text17.Text) / 3
Text18 = Val(Text18.Text) / 3
case 条件3
Text16 = Val(Text16.Text) / 4
Text17 = Val(Text17.Text) / 4
Text18 = Val(Text18.Text) / 4
end select
End Sub

zdingyun 2010-05-05
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Text16 = Val(Text1.Text) + Val(Text4.Text) + Val(Text7.Text) + Val(Text10.Text)
Text17 = Val(Text2.Text) + Val(Text5.Text) + Val(Text8.Text) + Val(Text11.Text)
Text18 = Val(Text3.Text) + Val(Text6.Text) + Val(Text9.Text) + Val(Text12.Text)
If Text7.Text = "" Then
Text8.Text = ""
Text9.Text = ""
Text16 = Val(Text16.Text) / 3
Text17 = Val(Text17.Text) / 3
Text18 = Val(Text18.Text) / 3
ElseIf XXXX Then 'XXXX为条件
Text10.Text = ""
Text11.Text = ""
Text12.Text = ""
Text16 = Val(Text16.Text) / 3
Text17 = Val(Text17.Text) / 3
Text18 = Val(Text18.Text) / 3
Else
Text16 = Val(Text16.Text) / 4
Text17 = Val(Text17.Text) / 4
Text18 = Val(Text18.Text) / 4
End If
End Sub

7,784

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧