Soru var olan numaranın girilmesine izin vermeyen userform

Katılım
29 Kasım 2018
Mesajlar
5
Excel Vers. ve Dili
Excel 2007 Türkçe
Merhaba, etkinlik için userformlu kayıt makrosu hazırlamaktayım. Bu userform üniversite, lise ve diğer öğrencilerden gerekli bilgileri alarak veri girişini son dolu satırı bularak bir sonraki boş satıra yazdıracak şekilde çalışmaktadır.Ancak command button 1 (click) için atadığım kodlarda bazı sorunlar var. textbox1 ve textbox6 gereken uzunluğa eşit olmadığı durumda userformda girilmiş verilerin temizlenmeden önce yanlışın olduğu textboxa setfocus ile odaklanarak düzeltilmesini istiyorum. ancak bu kod ile kaydete bastığım zaman userforma girilmiş verileri temizledikten sonra setfocus yapmakta. ayrıca textbox 1 için aynı veri daha önce girilmişse tekrar girilmesine engel olarak uyarı vermesi gerekmekte. fakat uyarı verse bile kaydete basınca tüm veriler sayfa1e aktarılmakta. hata varsa düzeltilebilsin ve sayfa1e girmesin. hata yoksa veriler safa1e aktarılsın istiyorum. excel dosyası ve sayfa1 görünümü 2 linkte paylaşılmıştır. sayfa1 görünüm excel dosyası



Kod:
Private Sub OptionButton1_Click()
a = Sayfa1.Cells(Rows.Count, 1).End(xlUp).Row
Sayfa1.Cells(a + 1, 3) = "Erkek"
End Sub

Private Sub OptionButton2_Click()
a = Sayfa1.Cells(Rows.Count, 1).End(xlUp).Row
Sayfa1.Cells(a + 1, 3) = "Kadın"
End Sub

Private Sub OptionButton3_Click()
TextBox1.Visible = True
Label1.Visible = True
TextBox4.Visible = True
Label4.Visible = True
Label7.Visible = False
ComboBox1.Visible = False
a = Sayfa1.Cells(Rows.Count, 1).End(xlUp).Row
Sayfa1.Cells(a + 1, 4) = "OMÜ"
End Sub

Private Sub OptionButton4_Click()
TextBox1.Visible = False
Label1.Visible = False
TextBox4.Visible = False
Label4.Visible = False
Label7.Visible = True
ComboBox1.Visible = True
a = Sayfa1.Cells(Rows.Count, 1).End(xlUp).Row
Sayfa1.Cells(a + 1, 4) = "Lise"
End Sub

Private Sub OptionButton5_Click()
TextBox1.Visible = False
Label1.Visible = False
TextBox4.Visible = False
Label4.Visible = False
Label7.Visible = False
ComboBox1.Visible = False
a = Sayfa1.Cells(Rows.Count, 1).End(xlUp).Row
Sayfa1.Cells(a + 1, 4) = "Diğer"
End Sub

Private Sub UserForm_Activate()
ComboBox1.List = Sayfa2.Range("a1:a4").Value
End Sub

Private Sub CommandButton1_Click()
a = Sayfa1.Cells(Rows.Count, 1).End(xlUp).Row
b = Len(TextBox1.Text)
c = Len(TextBox6.Text)

If b <> 8 Then
MsgBox "Okul Numarası 8 haneli olmalıdır"
TextBox1.SetFocus
End If
If c <> 4 Then
MsgBox "Doğum yılı 4 haneli olmalıdır"
TextBox6.SetFocus
End If

If IsNumeric(TextBox1.Text & TextBox6.Text) = False Then
MsgBox "Okul Numarası ve Doğum yılı sadece rakamlardan oluşmalıdır"
TextBox1.SetFocus
Else
Sayfa1.Cells(a + 1, 1) = TextBox2.Text
Sayfa1.Cells(a + 1, 2) = TextBox3.Text
Sayfa1.Cells(a + 1, 6) = TextBox4.Text
Sayfa1.Cells(a + 1, 7) = TextBox1.Text
Sayfa1.Cells(a + 1, 8) = TextBox6.Text
Sayfa1.Cells(a + 1, 5) = ComboBox1.Text
End If

TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox6.Text = ""
ComboBox1.Text = ""

For i = 2 To a
If Sayfa1.Cells(i, 7) = TextBox1.Text Then
MsgBox "Bu okul numarasına ait kayıt var. Lütfen numarayı kontrol et"
Exit For
Else
Sayfa1.Cells(a + 1, 1) = TextBox2.Text
Sayfa1.Cells(a + 1, 2) = TextBox3.Text
Sayfa1.Cells(a + 1, 6) = TextBox4.Text
Sayfa1.Cells(a + 1, 7) = TextBox1.Text
Sayfa1.Cells(a + 1, 8) = TextBox6.Text
Sayfa1.Cells(a + 1, 5) = ComboBox1.Text
End If
Next

End Sub
 
Üst