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

时间:2013-05-02 15:21:02

标签: excel excel-vba vba

1)我在VBA中创建了一个表单并且我已经连接到第一张表单,当我单击按钮时,表单会弹出以输入数据,但我想要的是,我想分配一个按钮第一张纸,当我点击按钮时,表格应该出现,当我在表格中插入数据时,它应该出现在第二张纸上。

2)我只将数据插入工作表四行,完成后如果想要修改特定列或特定数据行,我们怎么能这样做,我需要有人建议我,我也要求你给我发送代码如何修改。

3)如果我想输入新数据,我还要求您发送代码以清除工作表。

如果有人在这3点帮助我,我会很高兴。

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

1 个答案:

答案 0 :(得分:2)

A)在第一张中放置一个按钮。

如果您使用的是表单控件,请将此代码放在一个模块中,然后右键单击该按钮并单击assign macro,然后将其链接到以下代码

Sub Launch()
    UserForm1.Show
End Sub

enter image description here

如果您使用的是ActiveX控件,请在设计模式下双击它并使用此代码

Private Sub CommandButton1_Click()
    UserForm1.Show
End Sub

关于在Sheet2中显示数据,在表格​​的“Ok”按钮中,将其与第二张纸链接。例如。

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)为此,我建议先取用户的输入,然后根据该节目显示相关表格。

enter image description here

如果用户选择第一个选项然后显示您的数据输入用户形态,则显示编辑用户表格,该用户表格看起来像数据输入用户表格,但将有一个UserForm_Initialize(),它将从表单中提取数据,具体取决于用户是否需要编辑行或列。您如何选择接受该输入是我留给您的。例如

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