我得到运行时错误9当我试图用下面的代码执行程序。Excel VBA-运行时间错误9(下标超出范围)
Private Sub CommandButton1_Click()
Dim varResponse As Variant
varResponse = MsgBox("Are you sure want to add this ?", vbYesNo, "Selection")
If varResponse <> vbYes Then Exit Sub
Dim RowCount As Long
Dim ctl As Control
If Me.TextBox1.Value = "" Then
MsgBox "Please enter #.", vbOKOnly
Me.TextBox1.SetFocus
Exit Sub
End If
If Me.txtdescription.Value = "" Then
MsgBox "Please enter a description.", vbOKOnly
Me.txtdescription.SetFocus
Exit Sub
End If
' Write data to worksheet
RowCount = Worksheets("Secretarial Jobs Description").Range("A1").CurrentRegion.Rows.Count
With Worksheets("Secretarial Jobs Description").Range("A1")
.Offset(RowCount, 0).Value = Me.TextBox1.Value
.Offset(RowCount, 1).Value = Me.txtdescription.Value
End With
' 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
从而使调试部分强调,
RowCount = Worksheets("Secretarial Jobs Description").Range("A1").CurrentRegion.Rows.Count
With Worksheets("Secretarial Jobs Description").Range("A1")
是在错误已经找到。我的代码有错误吗?
是'现有的秘书职位Description'片。在你目前的工作手册中?您确定VBA代码和工作表本身之间没有拼写错误,补充/缺失白色字符或字母大小写区别? –
您是否正在运行与数据相同的工作簿或单独的宏? – tannman357
下面是知春里Excel对象 Sheet 1中(支付) Sheet2的(客户端) 表Sheet 3(工作表) Sheet4(用户) – user3776403