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

VBE指向导致错误的这一点代码:

Sheets("Topology").Range(Me.Get_Cell_Location).Interior.Color = RGB(255, 255, 255) 

整个function定义如下:

 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 

select1中的代码似乎工作正常,但select2给我的问题。

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无法确定范围。 我已经引入了error handling,并添加了一个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