所以我正在为我的课程编写一个程序而且非常卡住我把所有的值都放在了正确的单元格中,但是我想确保如果在那些已经存在的行之间有一行代码将在那里输入。到目前为止它只是寻找最后一排并将它放在那里,即使它们之间有空间。
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
这就是我的作品。另外,我如何确保如果用户放置一个特殊字符,例如“!”£$%^& *(){} []:; @'〜#?><,。/ | \“或一个文本框中的数字,显示一个消息框,说明其不正确我为数字做了但不知道如何为此做。
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"
这是我在提交按钮下面的所有内容所有验证检查工作都会给出消息,但是我提到的引用之后的最后一个代码根本不起作用。我需要做些什么才能完成这项工作
答案 0 :(得分:0)
雅杀了我的小伙伴&lt; 3
以下是您在模块/用户表单代码中应该查看的顺序中要求的所有内容。由于你是新手,因此值得指出执行的顺序很重要。一旦开始使用更大的对象集和更复杂的计算类型,故障排除通常需要确定执行顺序中设置的值的位置。这可能是我第一个更大的宏组中最头痛的问题。
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