我得到運行時錯誤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