2014-06-26 214 views
0

我得到运行时错误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") 

是在错误已经找到。我的代码有错误吗?

+0

是'现有的秘书职位Description'片。在你目前的工作手册中?您确定VBA代码和工作表本身之间没有拼写错误,补充/缺失白色字符或字母大小写区别? –

+0

您是否正在运行与数据相同的工作簿或单独的宏? – tannman357

+0

下面是知春里Excel对象 Sheet 1中(支付) Sheet2的(客户端) 表Sheet 3(工作表) Sheet4(用户) – user3776403

回答

0

“下标超出范围”是当通过项目的名称或索引找不到项目时生成的错误。很有可能您的当前工​​作簿中没有(完全)名为“秘书工作描述”的工作表。

0

就像已经说过的那样,它必须是您要求的工作表名称不正确或不存在。

你可以尝试引用片通过它的对象数:

RowCount = Sheets(1).Range("A1").CurrentRegion.Rows.count 

然后,它不会不管它叫什么,只是什么地方它是在

+0

我试图改变,但它仍然无法正常工作 – user3776403

相关问题