繁体   English   中英

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

[英]Application Defined or Object Defined Error in Excel with 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 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给我带来了问题。

UPDATE

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM