我正在尝试使用Excel和VBA创建表单。当我尝试提交表单时,我收到错误说“运行时错误91:
Object Variable or With block variable not set
。当我调试错误发生在第7行iRow= ws.Cell...
时,非常感谢任何帮助!下面是单击确定按钮的代码:
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a Name number
If Trim(Me.TextBox_Name.Value) = "" Then
Me.TextBox_Name.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.TextBox_Name.Value
ws.Cells(iRow, 2).Value = Me.TextBox_Desc.Value
ws.Cells(iRow, 3).Value = Me.Frame_Group.Value
ws.Cells(iRow, 4).Value = Me.ComboBox_Location.Value
ws.Cells(iRow, 5).Value = Me.Frame_Time.Value
ws.Cells(iRow, 6).Value = Me.Frame_Life.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.TextBox_Name.Value = ""
Me.TextBox_Desc.Value = ""
Me.Frame_Group.Value = ""
Me.ComboBox_Location.Value = ""
Me.Frame_Time.Value = ""
Me.Frame_Life.Value = ""
Me.TextBox_Name.SetFocus
End Sub
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
用这些代码替换你的行它的工作:)