簡體   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