用于自动化工作表的VBA代码

1)我已经在VBA中创build了一个表单,我已经连接到第一张表单,当我点击button时,表单popup来input数据,但是我想要的是,我想在第一个表单中分配一个button,当我点击button时,窗体应该出现,当我插入数据到窗体,它应该出现在第二张表。

2)我将数据插入表中只有四行,完成后,如果要修改特定的列或特定的数据行我们如何做到这一点,我需要有人build议我这一点,我也请求你发送我代码如何修改。

3)如果我想input新的数据,我还要求您发送代码清除表单。

如果有人帮我这三点,我会很高兴。

Private Sub cmdAdd_Click() Dim i As Integer 'position cursor in the correct cell B9. Range("B9:P40").Select i = 1 'set as the first ID 'validate first four controls have been entered... If Me.txtFName.Text = Empty Then 'Firstname MsgBox "Please enter firstname.", vbExclamation Me.txtFName.SetFocus 'position cursor to try again Exit Sub 'terminate here - why continue? End If If Me.txtSName.Text = Empty Then 'Surname MsgBox "Please enter surname.", vbExclamation Me.txtSName.SetFocus 'position cursor to try again Exit Sub 'terminate here - why continue? End If If Me.txtFuName.Text = Empty Then 'FullName MsgBox "Please enter fullname.", vbExclamation Me.txtFuName.SetFocus 'position cursor to try again Exit Sub 'terminate here - why continue? End If If Me.txtDName.Text = Empty Then 'Designation MsgBox "Please enter Designation.", vbExclamation Me.txtDName.SetFocus 'position cursor to try again Exit Sub 'terminate here - why continue? End If 'if all the above are false (OK) then carry on. 'check to see the next available blank row start at cell B9... Do Until ActiveCell.Value = Empty ActiveCell.Offset(1, 0).Select 'move down 1 row i = i + 1 'keep a count of the ID for later use Loop 'Populate the new data values into the 'Data' worksheet. ActiveCell.Value = i 'Next ID number ActiveCell.Offset(0, 1).Value = Me.txtFName.Text 'set col B ActiveCell.Offset(0, 2).Value = Me.txtSName.Text 'set col C ActiveCell.Offset(0, 3).Value = Me.txtFuName.Text 'set col D ActiveCell.Offset(0, 4).Value = Me.txtDName.Text 'set col E 'Clear down the values ready for the next record entry... Me.txtFName.Text = Empty Me.txtSName.Text = Empty Me.txtFuName.Text = Empty Me.txtDName.Text = Empty Me.txtFName.SetFocus 'positions the cursor for next record entry End Sub Private Sub cmdClose_Click() 'Close the form (itself) Unload Me End Sub 

A)在第一张表中放置一个button。

如果您使用表单控件,然后将此代码放在一个模块中,然后右键单击该button,然后单击分配macros,然后将其链接到下面的代码

 Sub Launch() UserForm1.Show End Sub 

在这里输入图像说明

如果您使用的是ActiveX控件,请在devise模式中双击并使用此代码

 Private Sub CommandButton1_Click() UserForm1.Show End Sub 

关于在Sheet2中显示数据,在表单的“确定”button中,将其链接到第二张表格。 例如。

 Private Sub CommandButton1_Click() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Sheet2") With ws '~~> Assuming you want to write to only 1st 4 rows as you mentioned .Range("A1").Value = TextBox1.Text .Range("A2").Value = TextBox2.Text .Range("A3").Value = TextBox3.Text .Range("A4").Value = TextBox4.Text End With End Sub 

B)为此,我会build议用户input,然后根据显示的相关表格。

在这里输入图像说明

如果用户select第一个选项,然后显示您的数据input用户窗体,否则显示编辑用户窗体,它将看起来像数据inputUserForm_Initialize()但将有一个UserForm_Initialize() ,将从表中拉取数据取决于用户是否想要编辑行或柱。 你如何select采取这种投入是我给你的。 例如

 Private Sub UserForm_Initialize() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Sheet2") With ws TextBox1.Text = .Range("A1").Value TextBox2.Text = .Range("A2").Value TextBox3.Text = .Range("A3").Value TextBox4.Text = .Range("A4").Value End With End Sub 

C)要清除表单,您可以使用此代码。

 Private Sub CommandButton2_Click() ThisWorkbook.Sheets("Sheet2").Cells.ClearContents End Sub