VBA döngülerinde yeniyim. Yapmaya çalıştığım şey, geçerli kod girilene kadar ürün kodu isteyen bir do döngü ve giriş kutusu prosedürüdür. Kod P harfiyle başlamalı ve ardından dört basamakla takip edilmelidir. Geçersiz bir kod girişi için, kullanıcıya neden geçersiz olduğunu bildiren bir mesaj göstermelidir.VBA Do Döngü ve Giriş Kutusu içeriği çalışmıyor
Aşağıdaki prosedür kodlanmış. Kullanıcı, örneğin p9887'u türettiğinde çalışır. kullanıcı türleri o899876 veya p877789, bu mesajı verirse
Ancak "Ürün Kodu beş karakter olmalıdır" ve ardından kullanıcı tekrar girişine sahiptir. Bu ikinci denemede, eğer kullanıcı tüm kriterleri karşılarken p9876 kullanıyorsa, benim prosedürümden gelen mesaj "Son dört karakter basamak olmalıdır" ve kullanıcının o kelimeyi yazmak zorunda olduğu döngüde kalıyor. tekrar giriş ve aynı mesaj geliyor.
Neyi yanlış yaptığımla ilgili herhangi bir kavrayış çok beğenildi!
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
************************ İşte temizleyici kod başka bir türüdür, ancak yine de aynı sorunu yok.
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
Bu problemini nasıl çözer? –
Ah ... Anlıyorum. Desen eşleşmesi tüm çeklerini eskimiş yapar. –
evet. Tabii ki gerektiği kadar "cluey" yapılabilir.Bu yönde bir düzenleme göndereceğim – user3598756