这段代码提示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
谢谢各位高手!
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
谢谢各位高手!
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
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
If Text7.Text = "" Then
……Else
……Else
……
End if
是错的
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
比如
if (xxx)
if (yyy)
...
else
...
其实应该是
if (xxx)
if (yyy)
...
else
...
因为编译器可不管缩进 :)
在VB6里面是
If xxx Then If yyy Then ... Else ...
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
如楼上提示,将中间的else加上if就可以了!