我试图创建一个代码,在工作表上显示姓名,联系人,地址,联系电话,电子邮件和产品订单的文本,但在输入所有信息后,我不断收到此编译错误用户表单并单击“确定”,注意:所有编码都在“确定”命令按钮上。
Private Sub cmdok_Click()
'activate worksheet
Worksheets("CustomersOrders").Activate
'check if row empty
NextRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
Cells(NextRow, 1) = txtName.Text
Cells(NextRow, 2) = txtperson.Text
Cells(NextRow, 3) = txtaddress.Text
Cells(NextRow, 4) = txtcontact.Text
Cells(NextRow, 5) = txtemail.Text
Cells(NextRow, 6) = txtorder.Text
If optYes Then
Worksheets("SupportInfo").Activate
NextRow
Application.WorksheetFunction.CountA (Range("A:A")) + 1
Cells(NextRow, 1) = txtName.Text
Cells(NextRow, 2) = txtperson.Text
Cells(NextRow, 3) = txtaddress
Cells(NextRow, 4) = txtcontact.Text
Cells(NextRow, 5) = txtemail.Text
Cells(NextRow, 6) = txtorder.Text
Cells(NextRow, 7) = txtdeldate.Text
End If
'clear the controls for next entry and set focus to Name
txtName.Text = ""
txtperson.Text = ""
txtaddress.Text = ""
txtcontact.Text = ""
txtemail.Text = ""
txtorder.Text = ""
txtdeldate.Text = ""
txtName.SetFocus
'hide the worksheets
Worksheets("CustomersOrders").Visible = False
Worksheets("SupportInfo").Visible = False
End Sub
答案 0 :(得分:0)
我认为问题是你试图调用NextRow,好像它是IF语句中的一个函数。你可能想要:
NextRow = Application.WorksheetFunction.CountA (Range("A:A")) + 1
注意:我编辑了您的问题以正确格式化代码,因此假设您的代码中实际上缺少分配。
答案 1 :(得分:0)
一般解决方案是here。我建议调试程序(使用F8)找出导致错误的行。正如我在评论中提到的,我怀疑 optYes 是一个CheckBox控件,你不能这样使用它。您需要检查值属性:
If optYes.Value then
'your code
End If
其他可能的问题:
1)您无法激活可见属性设置为 false 的工作表,您需要更改其可见性
'earlier
Worksheets("CustomersOrders").Visible = False
'later
Worksheets("CustomersOrders").Visible = True
Worksheets("CustomersOrders").Activate
2)您的代码不保证 NextRow 将返回正确的(第一个空)行;而不是它,使用函数:
Public Function GetNextEmptyRow(wsh As Worksheet, Optional sDefCol As String = "A") As Long
GetNextEmptyRow = wsh.Range(sDefCol & wsh.Rows.Count).End(xlUp).Row +1
End Function
'call
Dim nextRow As Long
nextRow = GetNextEmptyRow(Worksheets("CustomersOrders"))
3)我建议使用ADODB对存储在Excel工作表中的数据进行CRUD操作。
干杯,
马切伊