2016-04-10 15 views
0

VBAループで新しくなった。私がしようとしているのは、有効なものが入力されるまでプロダクトコードを尋ねるdoループと入力ボックスプロシージャです。コードは文字Pで始まり、4桁の数字が続くはずです。無効なコード入力の場合、それが無効である理由をユーザーに知らせるメッセージを表示する必要があります。VBAループと入力ボックスの内容が正しく動作しない

私は以下の手順をコードしています。例えば、ユーザがp9887と入力すると動作します。

ユーザーがタイプo899876またはp877789ただし、それは 「プロダクトコードが5つの文字を持たなければならない」とし、ユーザーが再び入力に持つメッセージ を与えます。この2回目の試行では、ユーザータイプp9876がすべての基準を満たしている間に、自分のプロシージャから表示されるメッセージが「最後の4文字は数字でなければなりません」というメッセージが表示され、ユーザーが入力しなければならないループ再度入力し、同じメッセージが表示されます。

私が間違っていることについての洞察は非常に高く評価されています!

Option Explicit 
Public Sub ProductCode() 


Dim strInput As String 

Dim intFrstLetter As Integer 

Dim intLastFour As String 

Dim strFrstLetter As String 

Dim test As String 

Dim blDone As Boolean 


strInput = InputBox("Please enter product code") 

intFrstLetter = InStr(1, strInput, "p") 

intLastFour = Right(strInput, 4) 

strFrstLetter = Left(strInput, 1) 

Do 

If strFrstLetter = "p" Then 

    If Len(strInput) <> 5 Then 
     MsgBox "Product code should have five characters." 
    strInput = InputBox("Please enter product code") 
    Else 
     If IsNumeric(intLastFour) Then 
     MsgBox "Thank You" 
     blDone = True 
     Exit Do 
     Else 
     MsgBox "The last four characters should be digits" 
     strInput = InputBox("Please enter product code") 
    If strFrstLetter <> "p" Then 
    MsgBox "Product code should start with the letter P" 
    strInput = InputBox("Please enter product code") 
     End If 
    End If 
    End If 
End If 
Loop Until blDone = True 
End Sub 

************************ここでは、クリーンなコードの別のタイプであるが、それでも同じ問題を行います。

Public Sub ProductCode() 
Dim strInput As String 
Dim intFrstLetter As Integer 
Dim intLastFour As String 
Dim strFrstLetter As String 
Dim blDone As Boolean 


strInput = InputBox("Please enter product code") 

intFrstLetter = InStr(1, strInput, "p") 
intLastFour = Right(strInput, 4) 
strFrstLetter = Left(strInput, 1) 
Do 
    If strFrstLetter = "p" Then 
     If Len(strInput) = 5 Then 
      If IsNumeric(intLastFour) = True Then 
      MsgBox "Thank You" 
      Exit Do 
      Else 
      MsgBox "The last four characters should be digits" 
      strInput = InputBox("Please enter product code") 
      End If 
     Else 
     MsgBox "Product code should have five characters" 
     strInput = InputBox("Please enter product code") 
     End If 
    Else 
    MsgBox "Product code should start with the letter P" 
    strInput = InputBox("Please enter product code") 
End If 
Loop 

答えて

1

以下の可能性があり、この

Do While Not blDone 
    blDone = InputBox("Please enter product code") Like "P####" 
    If Not blDone Then MsgBox "the input didn't match the pattern 'P####' where:" _ 
           & vbCrLf & vbCrLf & vbTab & "'P' must be the 'P' letter" _ 
           & vbCrLf & vbTab & "'####' must be four integer digits" 
Loop 

より「助けて」入力ブロックのコードを試してみてください。

Dim strInput As String, msgStrng As String, defStrng As String 
Dim blDone As Boolean 

defStrng = "P#### [enter digits for each '#']" 
Do While Not blDone 
    strInput = InputBox("Please enter product code", "Product Code input", defStrng) 
    blDone = strInput Like "P####" 
    If Not blDone Then 
     Select Case True 
      Case Len(strInput) <> 5 
       msgStrng = "Product code should have five characters" 
       defStrng = Left(strInput, 5) 
      Case Left(strInput, 1) <> "P" 
       msgStrng = "Product code should start with letter 'P'" 
       defStrng = "P" & Left(strInput, 4) 
      Case Else 
       msgStrng = "last four characters of Product code should be digits" 
       defStrng = strInput 
      End Select 

      MsgBox msgStrng, vbCritical 
    Else 
     MsgBox "Thank you" 
    End If 
Loop 
+0

これはどのようにして彼の問題を解決しますか? –

+0

ああ...そうだ。パターンの一致はすべての彼のチェックを廃止します。 –

+0

はい。もちろん、それは必要に応じて多くの "クライミー"にすることもできます。私はこの方向に編集を投稿します – user3598756

1

エラーが発生した後に新しい製品コードを入力しても、変数のうち3つは変更されません。 strInput = InputBox("Please enter product code")行をcall ProductCodeに変更します。このようにして、変数は新しい入力に応じて変更されます。

これらのバグを自分で理解するには、「デバッグ」メニューの「ステップイン」を使用して、コード内を移動しながら変数にマウスを移動します。または、プログラムの設計段階で、検証するコードの後に​​変数を表示します。その後、これらの不要な行が正常に機能していることを確認したら削除します。

+0

申し訳ありませんが、strInputのどの行をあなたの提案に変更する必要がありますか?私は自分のコードにいくつかのものを挙げました。また、これにより、ユーザーが製品コードを正しく入力できるようになりますか?たとえば、最初にpxyztと入力した場合、有効なコードではないことを示すメッセージが表示されます。次に、何か新しいものを入力する必要があります。したがって、p9876と入力すれば、「ありがとう」と言わなければなりません – Rosario

-1

私はこの問題を発見して、それを修正し、次の

strInput = InputBox("Please enter product code") 

Do 
If Left(strInput, 1) = "p" Then 
    If Len(strInput) = 5 Then 
     If IsNumeric(Right(strInput, 4)) = True Then 
      MsgBox "Thank You!" 
      blDone = True 
      Exit Do 
     Else 
      MsgBox "The last four characters should be digits" 
      strInput = InputBox("Please enter product code") 
     End If 
    Else 
     MsgBox "Product code should have five characters" 
     strInput = InputBox("Please enter product code") 
    End If 
Else 
    MsgBox "Product code should start with the letter P" 
    strInput = InputBox("Please enter product code") 
End If 

Loop Until blDone = True 

問題は、これらの変数を宣言し、このように設定してコードで使用していたことです。

intFrstLetter = InStr(1, strInput, "p") 
intLastFour = Right(strInput, 4) 
strFrstLetter = Left(strInput, 1) 

コードからこれらの変数を削除すると、ループは正常に機能します。 皆様のご返信ありがとうございます!

関連する問題