Excel用户表单 - 如何从电子表格中获取数据以再次以用户形式显示

时间:2014-07-08 15:49:20

标签: excel excel-vba vba

好的,我是VBA的新手。我在Excel中创建了一个用户表单,可以将数据正确地加载到电子表格中,并具有可用的命令按钮。但是,如何将一行数据添加到电子表格中以在表单中重新显示。我知道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

2 个答案:

答案 0 :(得分:1)

这是一个简单的例子:

先决条件:空工作表,其中包含名为“UserForm1”的用户表单,其中包含一个名为“TextBox1”的文本框和一个名为“Command Button1”的按钮。

场景:如果工作表上的选择发生更改,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

答案 1 :(得分:0)

在“yourbutton_click”结束时,您可以像

一样更新文本框
textbox.text = range("yourrange").value

希望它有所帮助,你也可以详细说明你的问题。