Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA的Excel中的应用程序定义或对象定义错误_Excel_Vba - Fatal编程技术网

使用VBA的Excel中的应用程序定义或对象定义错误

使用VBA的Excel中的应用程序定义或对象定义错误,excel,vba,Excel,Vba,VBE指向导致错误的此代码位: Sheets("Topology").Range(Me.Get_Cell_Location).Interior.Color = RGB(255, 255, 255) 整个功能定义如下: Public Function Colour_Me(choice As Integer) As Boolean If choice = 1 Then Debug.Print "Choice 1" If Me.Get_Enabled1 = True Or M

VBE指向导致错误的此代码位:

Sheets("Topology").Range(Me.Get_Cell_Location).Interior.Color = RGB(255, 255, 255)
整个功能定义如下:

Public Function Colour_Me(choice As Integer) As Boolean

If choice = 1 Then

    Debug.Print "Choice 1"

     If Me.Get_Enabled1 = True Or Me.Get_Enabled2 = True Or Me.Get_Enabled3 = True Then
        Sheets("Topology").Range(Me.Get_Cell_Location).Interior.Color = RGB(255, 255, 0)
        Colour_Me = True
     Else
        Sheets("Topology").Range(Me.Get_Cell_Location).Interior.Color = RGB(255, 255, 255)
        Colour_Me = False
     End If

ElseIf choice = 2 Then

      Debug.Print "Choice 2"

     If Me.Get_Enabled1 = True Or Me.Get_Enabled2 = True Or Me.Get_Enabled3 = True Then
        Sheets("Topology").Range(Me.Get_Cell_Location).Interior.Color = RGB(0, 0, 255)
        Colour_Me = True
     Else
        Sheets("Topology").Range(Me.Get_Cell_Location).Interior.Color = RGB(255, 255, 255)
        Colour_Me = False
     End If
End If

End Function
选项1中的代码似乎工作正常,但选项2给我带来了问题

更新

Public Property Let Set_Cell_Location(location As String)
    cell_location = location
End Property

Public Property Get Get_Cell_Location()
    Get_Cell_Location = cell_location
End Property

我相信您会遇到这个错误,因为Excel无法确定范围。我引入了错误处理并添加了一个MSGBOX。看看它给你带来了什么价值

试试这个

Public Function Colour_Me(choice As Integer) As Boolean
    Dim Rng As Range

    On Error GoTo Whoa

    Set Rng = Sheets("Topology").Range(Me.Get_Cell_Location)

    If choice = 1 Then
        Debug.Print "Choice 1"
        If Me.Get_Enabled1 = True Or Me.Get_Enabled2 = True Or _
        Me.Get_Enabled3 = True Then
           Rng.Interior.Color = RGB(255, 255, 0)
           Colour_Me = True
        Else
           Rng.Interior.Color = RGB(255, 255, 255)
           Colour_Me = False
        End If
    ElseIf choice = 2 Then
        Debug.Print "Choice 2"
        If Me.Get_Enabled1 = True Or Me.Get_Enabled2 = True Or _
        Me.Get_Enabled3 = True Then
           Rng.Interior.Color = RGB(0, 0, 255)
           Colour_Me = True
        Else
           Rng.Interior.Color = RGB(255, 255, 255)
           Colour_Me = False
        End If
    End If
    Exit Function
Whoa:
    '~~> I have just put this here for testing
    Msgbox Me.Get_Cell_Location
End Function

Colour\u Me
是方法名,不是变量,对吗?我对VB不太熟悉,但看起来你应该创建一个布尔变量,根据结果将其设置为true或false,然后将该变量返回给调用者。Get_Cell_Location是一个sub,其值应该是字符串(例如A3)。MSGBOX没有文本。我猜是我。Get\u Cell\u位置在子调用之前没有值?嗯,
Get\u Cell\u Location
Sub还是函数?您是如何拨打电话的?也许你想把这个值作为函数的一个参数?例如
Colour\u Me(选择为整数,添加为字符串)
Ok,这可能是问题所在:我在函数开头放置
Debug.Print Me.Get\u Cell\u Location
,在即时窗口中显示$J$9而不是J9。不是$不会给您带来问题。你能确认一下,如果是Sub或FunctionNvm Siddharth,我找到了问题所在。我没有初始化Get\u Cell\u位置试图从中访问的类。