Excel用户表单 – 如何从电子表格中获取数据以再次以用户forms显示
好的,我是VBA的新手。 我在Excel中创build了一个用户表单,可以正确地将数据加载到电子表格中,并具有可用的命令button。 但是,如何才能将一行数据添加到电子表格中以在表单中重新显示。 我知道Excel的窗体视图是这样的,但是框很小,看不到所有的数据。
Private Sub AbstractReceipt_Initalize() End Sub Private Sub AbstractReceipt_Click() 'ID = Sheet1.Range("A") 'Abstract_Receipt_No = Sheet1.Range("B") 'Date = Sheet1.Range("C") 'Attorney_Name = Sheet.Range("D") 'Client_Name = Sheet.Range("E") 'Abstract_Co_Name = Sheet.Range("F") 'Legal_Description = Sheet.Range("G") 'Title_Opinion_No = Sheet.Range("H") 'Signature_Information = Sheet.Range("I") 'Other_Names = Sheet.Range ("J") AbstractReceipt.IDBox.Text = ID AbstractReceipt.AbReceiptBox.Text = Abstract_Receipt_No AbstractReceipt.DateTextBox.Text = Date AbstractReceipt.AttorneyTextBox.Text = Attorney_Name AbstractReceipt.ClientTextBox.Text = Client_Name AbstractReceipt.AbCoTextBox.Text = Abstract_Co_Name AbstractReceipt.LegalTextBox.Text = Legal_Description AbstractReceipt.TOTextBox.Text = Title_Opinion_No AbstractReceipt.SigTextBox.Text = Signature_Information AbstractReceipt.OtherTextBox.Text = Other_Names End Sub Private Sub AbCoTextBox_Enter() AbCoTextBox.Text = "" End Sub Private Sub AbReceiptTextBox_Enter() AbReceiptTextBox.Text = "" End Sub Private Sub AttorneyTextBox_Enter() AttorneyTextBox.Text = "" End Sub Private Sub ClientTextBox_Enter() ClientTextBox.Text = "" End Sub Private Sub CommandButton1_Click() AbstractReceipt.PrintForm End Sub Private Sub CommandButton2_Click() ' Clear the form For Each ctl In Me.Controls If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then ctl.Value = "" ElseIf TypeName(ctl) = "CheckBox" Then ctl.Value = False End If Next ctl End Sub Private Sub CommandButton3_Click() Dim lRow As Long Dim lAutoNo As Long With Sheet1 lAutoNo = Application.WorksheetFunction.Max(Sheet1.Columns(1)) lAutoNo = lAutoNo + 1 bEditing = True lRow = .UsedRange.Rows.Count + 1 .Cells(lRow, 1).Value = lAutoNo .Cells(lRow, 2).Value = Me.AbReceiptTextBox.Value .Cells(lRow, 3).Value = Me.AttorneyTextBox.Value .Cells(lRow, 4).Value = Me.ClientTextBox.Value .Cells(lRow, 5).Value = Me.AbCoTextBox.Value .Cells(lRow, 6).Value = Me.LegalTextBox.Value .Cells(lRow, 7).Value = Me.AbCoTextBox.Value .Cells(lRow, 8).Value = Me.TOTextBox.Value .Cells(lRow, 9).Value = Me.SigTextBox.Value .Cells(lRow, 10).Value = Me.OtherTextBox.Value bEditing = True End With End Sub Private Sub DateTextBox_Enter() DateTextBox.Text = "" End Sub Private Sub Frame1_Click() End Sub Private Sub LegalTextBox_Enter() LegalTextBox.Text = "" End Sub Private Sub OtherTextBox_Enter() OtherTextBox.Text = "" End Sub Private Sub SigTextBox_Enter() SigTextBox.Text = "" End Sub Private Sub TOTextBox_Enter() TOTextBox.Text = "" End Sub Private Sub CancelCommandButton_Click() Unload Me End Sub
这里是一个简单的例子:
先决条件:具有名为“UserForm1”的用户表单的空工作表,其具有名为“TextBox1”的一个文本框和名为“Command Button1”的一个button。
scheme:如果在工作表上select更改,UserForm1与TextBox1中的ActiveCell值显示。 在CommandButton1上单击,UserForm1隐藏和卸载。
代码可以重复使用以满足特定的要求:
工作表代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) UserForm1.Show UserForm1.TextBox1.Value = ActiveCell.Value End Sub
UserForm1代码:
Private Sub CommandButton1_Click() UserForm1.Hide Unload UserForm1 End Sub
在“yourbutton_click”的末尾,您可以像更新文本框一样
textbox.text = range("yourrange").value
希望它有帮助,你也可以详细说明你的问题。