如何修复 Excel 中的运行时错误“91”
How to fix Runtime Error "91" in Excel
我创建了一个用户表单来为我将数据输入工作sheet。我正在尝试对其进行编码,以便将信息放入 selected worksheet。我有一个组合框,我可以在其中 select sheet 我希望它可以放入信息。我正在尝试将变量 ws 设置为组合框值,但出现运行时错误“91”,它说 "object variable or with block not set"。我遇到问题的代码如下。
Private Sub bttn_Submit_Click()
Dim iRow As Long
Dim ws As Worksheet
'set the variable for the worksheet
ws = ComboBox1.Value
'find first empty row in database
iRow = Columns("A").Find("*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a date
If Trim(Me.txt_Date.Value) = "" Then
Me.txt_Date.SetFocus
MsgBox "Please enter a Date"
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="password"
.Cells(iRow, 1).Value = Me.txt_Date.Value
.Cells(iRow, 2).Value = Me.txt_Invoice.Value
.Cells(iRow, 3).Value = Me.txt_Mileage.Value
.Cells(iRow, 5).Value = Me.CheckBox_Oil_Filter
.Cells(iRow, 7).Value = Me.CheckBox_Air_Filter
.Cells(iRow, 9).Value = Me.CheckBox_Primary_Fuel_Filter
.Cells(iRow, 11).Value = Me.CheckBox_Secondary_Fuel_Filter
.Cells(iRow, 13).Value = Me.CheckBox_Transmission_Filter
.Cells(iRow, 15).Value = Me.CheckBox_Transmission_Service
.Cells(iRow, 17).Value = Me.CheckBox_Wiper_Blades
.Cells(iRow, 19).Value = Me.CheckBox_Rotation_Rotated
.Cells(iRow, 21).Value = Me.CheckBox_New_Tires
.Cells(iRow, 23).Value = Me.CheckBox_Differential_Service
.Cells(iRow, 25).Value = Me.CheckBox_Battery_Replacement
.Cells(iRow, 27).Value = Me.CheckBox_Belts
.Cells(iRow, 29).Value = Me.CheckBox_Lubricate_Chassis
.Cells(iRow, 30).Value = Me.CheckBox_Brake_Fluid
.Cells(iRow, 31).Value = Me.CheckBox_Coolant_Check
.Cells(iRow, 32).Value = Me.CheckBox_Power_Steering_Check
.Cells(iRow, 33).Value = Me.CheckBox_A_Transmission_Check
.Cells(iRow, 34).Value = Me.CheckBox_Washer_Fluid_Check
' .Protect Password:="password"
End With
'clear the data
Me.txt_Date.Value = ""
Me.txt_Invoice.Value = ""
Me.txt_Mileage.Value = ""
Me.txt_Date.SetFocus
End Sub
我拿了你的一段代码并向你解释了不一致之处:
Dim ws As Worksheet
ws = ComboBox1.Value
第一行代码将 ws
声明为 Worksheet 对象。 VBA中的对象需要设置关键字Set
,当然数据类型也需要和声明相匹配。
另一方面,在第二个语句中,您将 ws
设置为 ComboBox1.Value
,这是一个类型 String
。所以:
1) 你得到错误的原因是 ws
是一个变量,由于它的声明,它期望得到一个 Worksheet
对象,但你却试图转换一个String
在里面;
2) 您可能想要做的是 在 ws
中包含名称为 ComboBox1.Value
的工作表对象;这个,在代码上,应该这样写:
Set ws = ThisWorkbook.Sheets(ComboBox1.Value)
您只需要将ws = ComboBox1.Value
行替换为上面的行即可。
请注意,一般来说:如果您声明一个 X 类型的变量,它将始终只能转换 X[=40= 类型的变量] 对象,除非你根本不声明它(非常糟糕的做法,之后你会得到错误)。
我创建了一个用户表单来为我将数据输入工作sheet。我正在尝试对其进行编码,以便将信息放入 selected worksheet。我有一个组合框,我可以在其中 select sheet 我希望它可以放入信息。我正在尝试将变量 ws 设置为组合框值,但出现运行时错误“91”,它说 "object variable or with block not set"。我遇到问题的代码如下。
Private Sub bttn_Submit_Click()
Dim iRow As Long
Dim ws As Worksheet
'set the variable for the worksheet
ws = ComboBox1.Value
'find first empty row in database
iRow = Columns("A").Find("*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a date
If Trim(Me.txt_Date.Value) = "" Then
Me.txt_Date.SetFocus
MsgBox "Please enter a Date"
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="password"
.Cells(iRow, 1).Value = Me.txt_Date.Value
.Cells(iRow, 2).Value = Me.txt_Invoice.Value
.Cells(iRow, 3).Value = Me.txt_Mileage.Value
.Cells(iRow, 5).Value = Me.CheckBox_Oil_Filter
.Cells(iRow, 7).Value = Me.CheckBox_Air_Filter
.Cells(iRow, 9).Value = Me.CheckBox_Primary_Fuel_Filter
.Cells(iRow, 11).Value = Me.CheckBox_Secondary_Fuel_Filter
.Cells(iRow, 13).Value = Me.CheckBox_Transmission_Filter
.Cells(iRow, 15).Value = Me.CheckBox_Transmission_Service
.Cells(iRow, 17).Value = Me.CheckBox_Wiper_Blades
.Cells(iRow, 19).Value = Me.CheckBox_Rotation_Rotated
.Cells(iRow, 21).Value = Me.CheckBox_New_Tires
.Cells(iRow, 23).Value = Me.CheckBox_Differential_Service
.Cells(iRow, 25).Value = Me.CheckBox_Battery_Replacement
.Cells(iRow, 27).Value = Me.CheckBox_Belts
.Cells(iRow, 29).Value = Me.CheckBox_Lubricate_Chassis
.Cells(iRow, 30).Value = Me.CheckBox_Brake_Fluid
.Cells(iRow, 31).Value = Me.CheckBox_Coolant_Check
.Cells(iRow, 32).Value = Me.CheckBox_Power_Steering_Check
.Cells(iRow, 33).Value = Me.CheckBox_A_Transmission_Check
.Cells(iRow, 34).Value = Me.CheckBox_Washer_Fluid_Check
' .Protect Password:="password"
End With
'clear the data
Me.txt_Date.Value = ""
Me.txt_Invoice.Value = ""
Me.txt_Mileage.Value = ""
Me.txt_Date.SetFocus
End Sub
我拿了你的一段代码并向你解释了不一致之处:
Dim ws As Worksheet
ws = ComboBox1.Value
第一行代码将 ws
声明为 Worksheet 对象。 VBA中的对象需要设置关键字Set
,当然数据类型也需要和声明相匹配。
另一方面,在第二个语句中,您将 ws
设置为 ComboBox1.Value
,这是一个类型 String
。所以:
1) 你得到错误的原因是 ws
是一个变量,由于它的声明,它期望得到一个 Worksheet
对象,但你却试图转换一个String
在里面;
2) 您可能想要做的是 在 ws
中包含名称为 ComboBox1.Value
的工作表对象;这个,在代码上,应该这样写:
Set ws = ThisWorkbook.Sheets(ComboBox1.Value)
您只需要将ws = ComboBox1.Value
行替换为上面的行即可。
请注意,一般来说:如果您声明一个 X 类型的变量,它将始终只能转换 X[=40= 类型的变量] 对象,除非你根本不声明它(非常糟糕的做法,之后你会得到错误)。