2016-04-10 16 views
0

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 

cevap

1

aşağıdaki gibi olabilir bu

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 

Daha "yardım" giriş bloğu kodunu deneyin:

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

Bu problemini nasıl çözer? –

+0

Ah ... Anlıyorum. Desen eşleşmesi tüm çeklerini eskimiş yapar. –

+0

evet. Tabii ki gerektiği kadar "cluey" yapılabilir.Bu yönde bir düzenleme göndereceğim – user3598756

1

Hatadan sonra yeni bir ürün kodu girmek, değişkenlerinizin 4'ünden 3'ünü değiştirmez. strInput = InputBox("Please enter product code") hattını call ProductCode olarak değiştirin. Bu şekilde değişkenleriniz yeni girdiye göre değişecektir.

Bu hataların kendiliğinden tespit edilmesi için kodun içinden geçerken "hata ayıklama" menüsünden "İçeri gir" seçeneğini kullanabilir ve değişkenlerinizin üzerine fareyi kullanabilirsiniz. Veya alternatif olarak, programınızın tasarım aşamasında, doğrulamak istediğiniz bir kod parçasından sonra değişkenlerinizi görüntüleyin. Sonra, çalıştığından emin olduğunuzda bu gereksiz satırları silin.

+0

Üzgünüm, strInput’un hangi satırı önerilerinize göre değiştirilmelidir? Kodumda birkaç tane var. Ayrıca, bunu yaptığınızda, kullanıcı ürün kodunda doğru şekilde yazacak mı? Örneğin, ilk kez pxyzt yazıyorlarsa, iletinin neden geçerli bir kod olmadığını göstermesini istiyorum. Sonra yeni bir şey yazmalılar, bu yüzden p9876 yazıyorlarsa, o zaman "teşekkür ederim" demeliler – Rosario

-1

Sorunu buldum ve onu sabit aşağıdadır:

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 

Sorun, bu değişkenleri bildirmek ve bunları bu şekilde ayarlamak ve kodda kullanmak zorundaydı.

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

Bu değişkenler koddan kaldırıldıktan sonra, döngü gerektiği gibi çalışır! Yanıtlarınız için herkese teşekkür ederiz!

İlgili konular