我有以下代码来完成用户表单,然后在ListBox中选择的相应工作表上提交信息。我有命令按钮,只有在从ListBox中选择了工作表时才提交信息。我需要为每个TextBox做同样的帮助,以便用户在提交之前必须完整填写表单。谢谢你的帮助。
Private Sub UserForm_Initialize()
Dim ws As Worksheet
CommandButton1.Enabled = False
For Each ws In ThisWorkbook.Worksheets
ListBox1.AddItem (ws.Name)
Next ws
End Sub
Private Sub ListBox1_Change()
CommandButton1.Enabled = ListBox1.ListIndex <> -1
End Sub
Private Sub CommandButton1_Click()
whichSheet = ListBox1.Value
Dim n As Integer
Do
n = n + 1
ListBox1.AddItem Sheets(n).Name
Loop Until n = Worksheets.Count
Worksheets(whichSheet).Activate
Dim lastrow
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastrow = lastrow + 1
Cells(lastrow, 1) = TextBox1
answer = MsgBox("Are you sure you want to add the record?", vbYesNo + vbQuestion, "Add Record")
If answer = vbYes Then
Cells(lastrow, 1) = TextBox1.Text
Cells(lastrow, 2) = TextBox2.Text
Cells(lastrow, 3) = TextBox4.Text
Cells(lastrow, 4) = TextBox5.Text
Cells(lastrow, 5) = TextBox6.Text
Else
Cells(lastrow, 1) = ""
Exit Sub
End If
End Sub