使用对象作为参数调用子例程时出现运行时错误 excel vba
Runtime error when calling a subroutine with an object as argument excel vba
场景
我有一个用户表单,它有一个带有一些选项的组合框。在同一个用户窗体中还有一个文本框。当我 select 组合框中的某个选项时,我需要禁用文本框并更改背景颜色。
我的代码
以下是我的代码。 poType
是组合框名称,unitPrice
是文本框名称
Public Sub poType_Change()
If mainPage.poType.Value = "FOC" Then
disabling (unitPrice)
Else
enabling (unitPrice)
End If
End Sub
以下是禁用和启用子程序
Sub disabling(ByVal objectToDisable As Object)
objectToDisable.Enabled = False
objectToDisable.BackColor = &H80000003
End Sub
Sub enabling(ByVal objectToEnable As Object)
objectToEnable.Enabled = True
objectToEnable.BackColor = &H80000005
End Sub
但是,当我执行这段代码时,它显示运行时错误(需要 424 对象)。有人知道原因吗?
能够找到这个问题的根本原因。上面的问题可以通过两种方式解决
方法一
调用子程序时需要添加call
Public Sub poType_Change()
If mainPage.poType.Value = "FOC" Then
call disabling (unitPrice)
Else
call enabling (unitPrice)
End If
End Sub
Sub disabling(ByVal objectToDisable As Object)
objectToDisable.Enabled = False
objectToDisable.BackColor = &H80000003
End Sub
Sub enabling(ByVal objectToEnable As Object)
objectToEnable.Enabled = True
objectToEnable.BackColor = &H80000005
End Sub
方法二
不要对参数使用括号。但是对于这种情况,不要在
前面加上call
Public Sub poType_Change()
If mainPage.poType.Value = "FOC" Then
disabling unitPrice
Else
enabling unitPrice
End If
End Sub
Sub disabling(ByVal objectToDisable As Object)
objectToDisable.Enabled = False
objectToDisable.BackColor = &H80000003
End Sub
Sub enabling(ByVal objectToEnable As Object)
objectToEnable.Enabled = True
objectToEnable.BackColor = &H80000005
End Sub
场景
我有一个用户表单,它有一个带有一些选项的组合框。在同一个用户窗体中还有一个文本框。当我 select 组合框中的某个选项时,我需要禁用文本框并更改背景颜色。
我的代码
以下是我的代码。 poType
是组合框名称,unitPrice
是文本框名称
Public Sub poType_Change()
If mainPage.poType.Value = "FOC" Then
disabling (unitPrice)
Else
enabling (unitPrice)
End If
End Sub
以下是禁用和启用子程序
Sub disabling(ByVal objectToDisable As Object)
objectToDisable.Enabled = False
objectToDisable.BackColor = &H80000003
End Sub
Sub enabling(ByVal objectToEnable As Object)
objectToEnable.Enabled = True
objectToEnable.BackColor = &H80000005
End Sub
但是,当我执行这段代码时,它显示运行时错误(需要 424 对象)。有人知道原因吗?
能够找到这个问题的根本原因。上面的问题可以通过两种方式解决
方法一
调用子程序时需要添加call
Public Sub poType_Change()
If mainPage.poType.Value = "FOC" Then
call disabling (unitPrice)
Else
call enabling (unitPrice)
End If
End Sub
Sub disabling(ByVal objectToDisable As Object)
objectToDisable.Enabled = False
objectToDisable.BackColor = &H80000003
End Sub
Sub enabling(ByVal objectToEnable As Object)
objectToEnable.Enabled = True
objectToEnable.BackColor = &H80000005
End Sub
方法二
不要对参数使用括号。但是对于这种情况,不要在
前面加上callPublic Sub poType_Change()
If mainPage.poType.Value = "FOC" Then
disabling unitPrice
Else
enabling unitPrice
End If
End Sub
Sub disabling(ByVal objectToDisable As Object)
objectToDisable.Enabled = False
objectToDisable.BackColor = &H80000003
End Sub
Sub enabling(ByVal objectToEnable As Object)
objectToEnable.Enabled = True
objectToEnable.BackColor = &H80000005
End Sub