将值输入下一个空行+;VBA用户表单文本框的有效性检查

将值输入下一个空行+;VBA用户表单文本框的有效性检查,vba,cell,spreadsheet,userform,Vba,Cell,Spreadsheet,Userform,因此,我正在为我的课程编写一个程序,我被卡住了,我把我所有的值都输入到正确的单元格中,但我想确保,如果在已经存在的单元格之间有一行是空闲的,那么代码将在那里输入。到目前为止,它只是寻找最后一排,并把它放在那里,即使中间有一个空间 Dim LastRow As Long, ws As Worksheet Set ws = Sheets("Details") LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the

因此,我正在为我的课程编写一个程序,我被卡住了,我把我所有的值都输入到正确的单元格中,但我想确保,如果在已经存在的单元格之间有一行是空闲的,那么代码将在那里输入。到目前为止,它只是寻找最后一排,并把它放在那里,即使中间有一个空间

Dim LastRow As Long, ws As Worksheet

Set ws = Sheets("Details")

LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row

ws.Range("A" & LastRow).Value = Forename.Value 'Adds the TextBox into Col A & Last Blank Row
ws.Range("B" & LastRow).Value = Surname.Value
ws.Range("C" & LastRow).Value = School.Value
ws.Range("E" & LastRow).Value = Candidate.Value
这就是我的作品。另外,我如何确保如果用户输入特殊字符,例如“!”£$%^&*({}[]:;@'~#?> Ya killin me smalls 90)和(KeyAscii<97或KeyAscii>122),然后KeyAscii=0 端接头
谢谢,这非常有帮助,因为除了金额验证之外,我现在不需要这些验证。顺便问一下,有没有一种方法可以限制用户在此代码中只输入不超过4个数字。这两者之间会有什么变化,比如说,如果我要限制某人只允许写字母,我知道我可能需要做vbKeyA到vbKeyZ,而不是vbKey0到vbKey9,但是我还需要做什么改变,因为如果我只做了改变,就不允许我写任何东西。让我看看更多的内容,然后回复你@KarolKowalczyk@KarolKowalczyk我认为这应该是非常令人满意的。谢谢这位优秀的男士,他帮了我很多。在userform initialize中有一个问题,当我按下tab键时,我将焦点设置为我的一个文本字段名,然后直接提交,然后重置,然后关闭姓氏和文本框,如下所示。如何更改此@DougCoatsput ws。在胶印品前面。老实说,您现在应该有足够的资源来解决这个问题:)Range(“A:A”).End(xlDown).ws.Offset(-1,0).Value=“Forename”如下所示
If Len(Candidate.Value) > 4 Then
MsgBox "The Candidate number is too long"
End If

If IsNumeric(Candidate.Value) = False Then
MsgBox "Candidate number contains characters other than numbers"
End If
Private Sub Submit_click()
      'Output all information into the spreadsheet

If Forename.Value = "" Then
    Me.Forename.SetFocus
    MsgBox "The Forename is Missing"        'Validation Check - Makes sure the Value is not empty
End If

If Surname.Value = "" Then
    Me.Surname.SetFocus
    MsgBox "The Surname is Missing"         'Validation Check - Makes sure the Value is not empty
End If

If School.Value = "" Then
    Me.School.SetFocus
    MsgBox "The School you previously attended to is Missing"          'Validation Check - Makes sure the Value is not empty
End If

If Candidate.Value = "" Then
    Me.Candidate.SetFocus
    MsgBox "The Candidate number is Missing"         'Validation Check - Makes sure the Value is not empty
End If

If IsNumeric(Candidate.Value) = False Then
    MsgBox "Candidate number contains characters other than numbers"        'Validation Check - makes sure only numbers are entered
End If

If Trim(Me.Candidate.TextLength > 4) Then
    Me.Candidate.SetFocus
    MsgBox ("Candidate Number Contains more than 4 characters")   'Validation Check - Makes sure that no more than 4 characters are entered
End If

If Trim(Me.Candidate.TextLength < 4) Then
    Me.Candidate.SetFocus
    MsgBox ("Candidate Number Contains less than 4 characters")       'Validation Check - Makes sure that no less than 4 characters are entered
End If

Dim LastRow As Long, ws As Worksheet

Set ws = Sheets("Details")

LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row

'This is the stuff i had before that are quoted out i dont know what to use

'ws.Range("A" & LastRow).Value = Forename.Value 'Adds the TextBox into Col A & Last Blank Row
'ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Value = Forename.Value
'ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(0, 1).Value = Surname.Value
'ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(0, 2).Value = School.Value
'ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(0, 3).Value = Candidate.Value

 'Sub fillstuff()

Range("A:a").End(xlDown).Offset(-1, 0).Value = "Forename"
Range("A:a").End(xlDown).Offset(-1, 1).Value = "Surname"
Range("A:a").End(xlDown).Offset(-1, 2).Value = "School"
Range("A:a").End(xlDown).Offset(-1, 3).Value = "Candidate"
Sub CommandButton1_Click()

Dim somestuff As somestuff

'this checks textbox1 for more than 4 digits in length
 If Trim(Me.TextBox1.TextLength > 4) Then
     Me.TextBox.SetFocus
     MsgBox ("you have entered more than 4 characters")
     Exit Sub
 End If

'Some other code
'more code to do stuff

Range("A:a").End(xlDown).Offset(-1, 0).Value = "Forename"
Range("A:a").End(xlDown).Offset(-1, 1).Value = "Surname"
Range("A:a").End(xlDown).Offset(-1, 2).Value = "School"
Range("A:a").End(xlDown).Offset(-1, 3).Value = "Candidate"

'This makes the userform "disappear" after execution. Might Not be needed for your purpose
Unload Me

End Sub
Sub CommandButton2_Click()

'as a habit i tend to create "clear" command buttons as well
TextBox1.Value = vbNullString
TextBox2.Value = vbNullString 'also its a good habit to use vbNullString instead of ""

End Sub
'this prevents user from entering nonnumerics in textbox 1
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
     Select Case KeyAscii
       Case vbKey0 To vbKey9, vbKeyBack, vbKeyClear, vbKeyLeft, _
  vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
       Case Else
         KeyAscii = 0
         Beep
     End Select
End Sub
  'this assumes that the text box for letters only is a different text box. You would_
     ' need to duplicate this type of data validation per text box
     Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
          If (KeyAscii < 65 Or KeyAscii > 90) And (KeyAscii < 97 Or KeyAscii > 122) Then KeyAscii = 0
     End Sub