はじめまして、VB初心者です。
error 9 : subscript out of range が**のラインで出てしまいます。
どうしてなのかさっぱり分かりません。ご回答よろしくお願いします。
Private Sub GAP_Next_Button_Click()
Dim BreakPoint() As Boolean
Dim OktoGo As Boolean
Dim a, b, c As String
a = GAP_A_1_Textbox.Value
b = GAP_A_2_Textbox.Value
c = GAP_A_3_Textbox.Value
If Not BreakPoint(a, b, c) Then**下記のFunctionがおかしいのでしょうか?
OktoGo = False
MsgBox "false"
End If
GAP_Form.Hide
Weights_Scores_Form.Show
End If
End Sub
Private Function BreakPoint(a As String, b As String, c As String)
If Not IsNumeric(a) Or Not IsNumeric(b) Or Not IsNumeric(c) Then
MsgBox "Enter numeric. Please, try again.", vbExclamation + vbOKOnly,
"Input Error"
BreakPoint = False
Exit Function
If a <= 0 Then
MsgBox "Break Point One has to be smaller than BP2, BP3 and 1. Please,
try again.", vbExclamation + vbOKOnly, "Input Error"
BreakPoint = False
Exit Function
End If
If b <= a Then
MsgBox "Break Point One has to be smaller than BP2, BP3 and 1. Please,
try again.", vbExclamation + vbOKOnly, "Input Error"
BreakPoint = False
Exit Function
End If
If 1 <= c Then
MsgBox "Break Point One has to be smaller than BP2, BP3 and 1. Please,
try again.", vbExclamation + vbOKOnly, "Input Error"
BreakPoint = False
Exit Function
End If
If a = "" Or b = "" Or c = "" Then
MsgBox "Enter numeric. Please, try again.", vbExclamation + vbOKOnly,
"Input Error"
BreakPoint = False
Exit Function
End If
End Function
If Not BreakPoint(a, b, c) Then**下記のFunctionがおかしいのでしょうか?
OktoGo = False
MsgBox "false"
End If
GAP_Form.Hide
Weights_Scores_Form.Show
End If
の一番最後のEnd Ifは必要?
If Not IsNumeric(a) Or Not IsNumeric(b) Or Not IsNumeric(c) Then
MsgBox "Enter numeric. Please, try again.", vbExclamation + vbOKOnly,
"Input Error"
BreakPoint = False
Exit Function
の後にはEnd Ifがないのでは?
Simiさん、お返事ありがとうございます。
End ifですが、両方ともコピペミスです。
すいません。
正しいプログラム載せて
Dim a, b, c As String
↓
Dim a as String, b as String, c as String
あと関数BreakPoint内のIf文で比較している所は
ちゃんと型を揃えるべきです。
Private Sub GAP_Next_Button_Click()
Dim BreakPoint() As Boolean
Dim OktoGo As Boolean
Dim a, b, c As String
a = GAP_A_1_Textbox.Value
b = GAP_A_2_Textbox.Value
c = GAP_A_3_Textbox.Value
If Not BreakPoint(a, b, c) Then**下記のFunctionがおかしいのでしょうか?
OktoGo = False
MsgBox "false"
End If
GAP_Form.Hide
Weights_Scores_Form.Show
End Sub
Private Function BreakPoint(a As String, b As String, c As String)
If Not IsNumeric(a) Or Not IsNumeric(b) Or Not IsNumeric(c) Then
MsgBox "Enter numeric. Please, try again.", vbExclamation + vbOKOnly,
"Input Error"
BreakPoint = False
Exit Function
End If
If a <= 0 Then
MsgBox "Break Point One has to be smaller than BP2, BP3 and 1. Please,
try again.", vbExclamation + vbOKOnly, "Input Error"
BreakPoint = False
Exit Function
End If
If b <= a Then
MsgBox "Break Point One has to be smaller than BP2, BP3 and 1. Please,
try again.", vbExclamation + vbOKOnly, "Input Error"
BreakPoint = False
Exit Function
End If
If 1 <= c Then
MsgBox "Break Point One has to be smaller than BP2, BP3 and 1. Please,
try again.", vbExclamation + vbOKOnly, "Input Error"
BreakPoint = False
Exit Function
End If
If a = "" Or b = "" Or c = "" Then
MsgBox "Enter numeric. Please, try again.", vbExclamation + vbOKOnly,
"Input Error"
BreakPoint = False
Exit Function
End If
End Function
Simiさん、K.K.さんご指摘ありがとうございます。
全コード乗せました。
Dim a as String, b as String, c as String
と直しましたが、まだ同じエラーが出てしまいます。
Simiさんがおっしゃられる「形をそろえる」とはどういうことでしょうか?
具体的に教えていただけますか?
型をそろえるというのは、文字列で渡されている引数の比較を数値で比較を行っているからではないでしょうか。
あとエラーが起こっているのは、関数名が悪いみたいです。
BreakPoint以外の関数名にしてみてください。
文字列変数(a,b,c)を数値と比較しているところです。
あと戻り値の型が定義されてないです。
Private Function BreakPoint(a As String, b As String, c As String)
↓
Private Function BreakPoint(a As String, b As String, c As String) _
As Boolean
> Private Sub GAP_Next_Button_Click()
> Dim BreakPoint() As Boolean
ここで、「BreakPoint」という変数を定義しているので、その下の
> If Not BreakPoint(a, b, c) Then
という文は、
> Private Function BreakPoint(a As String, b As String, c As String)
という Functionプロシージャではなく、変数の方の BreakPointだと思われていますよ。
皆さん、ご助言ありがとうございます。
おかげさまで解決しました。
ツイート | ![]() |