0

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
4

1 回答 1

2

我相信您会收到该错误,因为 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
于 2012-06-13T18:04:37.230 回答