Excel 用于自动化图纸的VBA代码
1) 我已经在VBA中创建了一个表单,并与第一张表单建立了连接,当我单击按钮时,表单将弹出以输入数据,但我想要的是,我想在第一张表单中指定一个按钮,当我单击按钮时,表单应显示,当我将数据插入表单时,表单应显示在第二张表单中 2) 我插入到表中的数据只有四行,完成后,如果要修改特定列或特定行的数据,我们如何才能做到这一点,我需要有人建议我在这方面,我也要求你给我发送代码如何修改 3) 如果我想输入新数据,我还要求你给我发送代码来清除表格 如果有人在这三点上帮助我,我会很高兴Excel 用于自动化图纸的VBA代码,excel,vba,Excel,Vba,1) 我已经在VBA中创建了一个表单,并与第一张表单建立了连接,当我单击按钮时,表单将弹出以输入数据,但我想要的是,我想在第一张表单中指定一个按钮,当我单击按钮时,表单应显示,当我将数据插入表单时,表单应显示在第二张表单中 2) 我插入到表中的数据只有四行,完成后,如果要修改特定列或特定行的数据,我们如何才能做到这一点,我需要有人建议我在这方面,我也要求你给我发送代码如何修改 3) 如果我想输入新数据,我还要求你给我发送代码来清除表格 如果有人在这三点上帮助我,我会很高兴 Private Sub
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)在第一页中放置一个按钮
如果您使用的是表单控件,请将此代码放置在模块中,然后右键单击按钮并单击“分配宏”,然后将其链接到下面的代码
Sub Launch()
UserForm1.Show
End Sub
如果您使用的是ActiveX控件,则在设计模式下双击它并使用此代码
Private Sub CommandButton1_Click()
UserForm1.Show
End Sub
关于在表单2中显示数据,请在表单的“确定”按钮中,将其与第二张表单链接。比如说
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)对于这一点,我建议用户输入,然后根据输入显示相关表格 如果用户选择第一个选项,则显示数据输入用户表单,否则显示编辑用户表单,该表单看起来像数据输入用户表单,但有一个
Userform\u 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
你的评论真的很有创意:-)也许你最好的方法是创建一个简单的类来充当工作表和表单之间的接口。这将保留在内存中输入的值,直到应用程序退出—允许您询问的编辑选项。如果您还创建了一个泛型集合类来保存包含其所在单元格键值的条目,您可以通过单元格地址访问这些条目,以保持它们的正确性..只是我的想法--谢谢您的回复.嗨,谢谢您的回复。我已经实现了第三个(C)代码,它工作正常,但当我尝试使用代码(A)时,它现在显示在窗体上,我收到一条消息:(无法运行宏“Marco-Copy.xlsm”!button1click'。该宏可能在此工作簿中不可用,或者所有宏都可能被禁用。)你能帮我解决这个错误信息吗?我对be点有一个想法,但不知道如何实现它,我的想法是我想放置一个名为“编辑”的按钮,如果我点击该按钮,它将显示输入的行的复选框。帮助我。我对(B)点有一个想法但是我不知道如何实现它,我的想法是我想放置一个名为Edit的按钮,如果我单击该按钮,它将显示所输入行的复选框。请帮我解决这个问题。请帮我解决这个问题。