i getting run time error 9 when i trying to execute the program with the following code.
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
Whereby the debug part highlighted that
RowCount = Worksheets("Secretarial Jobs Description").Range("A1").CurrentRegion.Rows.Count
With Worksheets("Secretarial Jobs Description").Range("A1")
is where the error had found. Am i having mistake on the code?
"Subscript out of range" is the error generated when an item is not found into a collection by its name or its index. It's highly likely that there is no worksheet in your current workbook that is (exactly) named "Secretarial Jobs Description".
Like has been said, it must be that the sheet name you're calling for is incorrect or doesn't exist.
You could try referencing the sheet by it's object number:
RowCount = Sheets(1).Range("A1").CurrentRegion.Rows.count
Then it won't matter what it's called, just what place it is in.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.