陷入运行时错误" 1004" - 范围方法失败

时间:2014-04-19 19:39:05

标签: excel vba runtime-error

我遇到了这个问题。我正在使用Excel VBA中的各种代码进行实验。我的问题是,很多时候我做了一些非法的事情,并且经常在尝试很多事情时,我得到运行时错误1004.

我的问题是,在那之后,无论我做什么都无法运行,我根本无法做任何事情,包括一些代码,除非我重新启动Excel。有没有办法摆脱这种情况,而不必每次都重启Excel?重新设置和破坏代码也无济于事。

编辑:请求后我发布了整个代码。我此刻没有错误,但是如果我遇到一个新错误,我会发布导致它的代码。

Sub tp() 'for test
    Image5.Shadow = True

End Sub

Private Sub Card1_Click()

End Sub

Sub Image1_Click()
    Call NewCard(1)
    'Range("L22").Value = 1
End Sub

Private Sub Image2_Click()

End Sub

Private Sub Image3_Click()

End Sub


Private Sub Image4_Click()

End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("I3,I7,I11,I15,P3,P7,P11,P15")) Is Nothing Then


        If Sheets("TheChamberOfSecrets").Range("H1").Value = 0 Then
            Sheets("TheChamberOfSecrets").Range("H1").Value = 1
            If IsNumeric(Target.Value) And (Not IsEmpty(Target)) Then
                If (Int(Target.Value) = Target.Value) Then
                    tmp = Target.Address(RowAbsolute:=False, ColumnAbsolute:=False)
                    'Target.Value = Target.Value
                    'Target.Value = Int(Target.Value)
                    Sheets("TheChamberOfSecrets").Range(tmp).Value = Target.Value
                Else
                    tmp = Target.Address(RowAbsolute:=False, ColumnAbsolute:=False)
                    Target.Value = Sheets("TheChamberOfSecrets").Range(tmp).Value
                End If
            ElseIf IsEmpty(Target) Then
                Target.Value = 0
                tmp = Target.Address(RowAbsolute:=False, ColumnAbsolute:=False)
                Sheets("TheChamberOfSecrets").Range(tmp).Value = Target.Value
            Else
                tmp = Target.Address(RowAbsolute:=False, ColumnAbsolute:=False)
                Target.Value = Sheets("TheChamberOfSecrets").Range(tmp).Value
            End If
            Sheets("TheChamberOfSecrets").Range("H1").Value = 0
        End If




    End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
    If Not Intersect(Target, Range("L24:N24")) Is Nothing Then

        Call Locky
        Call Judge(Action(Target))

    ElseIf Not Intersect(Target, Range("K22")) Is Nothing Then

        Call up(1)

    ElseIf Not Intersect(Target, Range("N22")) Is Nothing Then

        Call up(2)

    ElseIf Not Intersect(Target, Range("K23")) Is Nothing Then

        Call down(1)

    ElseIf Not Intersect(Target, Range("N23")) Is Nothing Then

        Call down(2)

    ElseIf Not Intersect(Target, Range("J22")) Is Nothing Then

        Call shiftColour(1)

    ElseIf Not Intersect(Target, Range("O22")) Is Nothing Then

        Call shiftColour(2)

    ElseIf Not Intersect(Target, Range("I22")) Is Nothing Then

        Call RandomCard(1)

    ElseIf Not Intersect(Target, Range("P22")) Is Nothing Then

        Call RandomCard(2)

    ElseIf Not Intersect(Target, Range("I22")) Is Nothing Then

        'Range("G22").Value = "PENTE"
        RandomCard (1)

    ElseIf Not Intersect(Target, Range("P22")) Is Nothing Then
        RandomCard (2)

    ElseIf ((Not Intersect(Target, Range("I19")) Is Nothing) Or (Not Intersect(Target, Range("J19")) Is Nothing)) Then
        Call RandomHand

    ElseIf Not Intersect(Target, Range("H2,H6,H10,H14,Q2,Q6,Q10,Q14")) Is Nothing Then

        'Range("Player_2").Value = "lolwtfpwned"
        If Target.Value = "Active" Then

            Target.Interior.ColorIndex = 3
            Target.Value = "Inactive"
        Else
            Target.Interior.ColorIndex = 43
            Target.Value = "Active"
        End If
    ElseIf Not Intersect(Target, Range("I4,I8,I12,I16,P4,P8,P12,P16,O21")) Is Nothing Then

        Cells((Target.Row - 1), Target.Column).Value = RandomMoney()

    ElseIf Not Intersect(Target, Range("J2,J6,J10,J14,O2,O6,O10,O14")) Is Nothing Then

        Call ChangeDealer(Target)

    End If


End If
End Sub
Private Sub ChangeDealer(ByVal Target As Range)
    If Target.Address(RowAbsolute:=False, ColumnAbsolute:=False) <> Sheets("TheChamberOfSecrets").Range(DealerHolder).Value Then
        'Call ClearBlinds(Target)
        Call SetDealer(Target)
    End If

End Sub
Private Sub ClearBlinds(ByVal Target As Range)

End Sub

Private Sub SetDealer(ByVal Target As Range)
    DaAd = Target.Address(RowAbsolute:=False, ColumnAbsolute:=False)
    Sheets("TheChamberOfSecrets").Range(DealerHolder).Value = DaAd
    StartingPoint = Sheets("TheChamberOfSecrets").Range(DaAd).Value
    'Range("H1").Value = StartingPoint
    Target.Value = "       D"
    i = StartingPoint - 1
    If i < 1 Then
        i = i + 8
    End If
    DaAd = Sheets("TheChamberOfSecrets").Range(PlayerArray & i).Value
    Range(DaAd).Value = "      SB"
    i = i - 1
    If i < 1 Then
        i = i + 8
    End If
    DaAd = Sheets("TheChamberOfSecrets").Range(PlayerArray & i).Value
    Range(DaAd).Value = "      BB"


End Sub
Private Function RandomMoney() As Integer
    Call Randomize
    RandomMoney = (Int((15 - 3 + 1) * Rnd + 3)) * 100
End Function

Sub RandomHand()
    n1 = Int((52 - 1 + 1) * Rnd + 1)
    n2 = Int((51 - 1 + 1) * Rnd + 1)
    If n2 >= n1 Then
        n2 = n2 + 1
    End If

    Range(Card(1)).Value = n1
    Range(Card(2)).Value = n2

    Call UpdateCardImages


End Sub

Sub RandomCard(ByVal which As Integer)
    Call Randomize
    'randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
    'Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
    'Value = CInt(Math.Floor((51 - 1 + 1) * Rnd())) + 1
    Value = Int((51 - 1 + 1) * Rnd + 1)
    If which = 1 Then
        other = 2
    Else
        other = 1
    End If
    If Value >= Range(Card(other)).Value Then
        Value = Value + 1
    End If

    Range(Card(which)).Value = Value

    Call UpdateCardImages

End Sub

Sub shiftColour(ByVal which As Integer)
    Dim tempo As Integer
    Dim base As Integer
    tempo = Range(Card(which)).Value - 1
    colour = tempo Mod 4
    base = (tempo \ 4) * 4
    colour = (colour + 1) Mod 4
    Range(Card(which)).Value = base + colour + 1

    Call UpdateCardImages

End Sub
Sub up(ByVal which As Integer)

    Range(Card(which)).Value = Range(Card(which)).Value + 4

    If Range(Card(which)).Value > 52 Then
        Range(Card(which)).Value = Range(Card(which)).Value - 52
    End If


    Call UpdateCardImages

End Sub
Sub down(ByVal which As Integer)

    Range(Card(which)).Value = Range(Card(which)).Value - 4

    If Range(Card(which)).Value < 1 Then
        Range(Card(which)).Value = Range(Card(which)).Value + 52
    End If

    Call UpdateCardImages

End Sub

Function Card(ByVal num As Integer) As String
    If num = 1 Then
        Card = "L22"
    ElseIf num = 2 Then
        Card = "M22"
    End If
End Function


Function Action(ByVal Target As Range) As Integer 'Define Action Chosen

    If Target = Range("L24") Then
            Range("N25").Value = 1
            Action = 1

    ElseIf Target = Range("M24") Then
            Range("N25").Value = 2
            Action = 2
    Else
            Range("N25").Value = 3
            Action = 3
    End If
End Function
Sub Locky() 'Lock Fields
    Range("L26").Value = 1
End Sub
Sub Unlocky() 'Unlock Fields
    Range("L26").Value = 0

End Sub

Sub Judge(ByVal Action As Integer) 'Analyse Situation and define verdict
    If Range("L25").Value = "Right" Then
        Range("L25").Value = "Wrong"
    Else
        Range("L25").Value = "Right"
    End If
End Sub

Sub ChangeHand(ByVal Sel As Integer)
    Sheets("Sheet1").Range(PriorityCell).Value = Sel
    Call UpdateCardImages
    Call SwitchPriority

End Sub
Private Sub NewCard(ByVal Sel As Integer)
    If ((Range("L22").Value <> Sel) And (Range("M22").Value <> Sel)) Then
        ChangeHand (Sel)
        'Range("L22").Value = 1
    Else
        Call SwitchCards
    End If
End Sub
Private Sub SwitchPriority()
    If PriorityCell = "L22" Then
        Sheets("Sheet1").Range(PriorityHolder).Value = "M22"
        Range("M23").Value = "        ^"
        Range("L23").Value = ""
    Else
        Sheets("Sheet1").Range(PriorityHolder).Value = "L22"
        Range("L23").Value = "        ^"
        Range("M23").Value = ""
    End If
End Sub

Private Function PriorityCell() As String

    PriorityCell = Sheets("Sheet1").Range(PriorityHolder).Value

End Function
Private Sub UpdateCardImages()
    Call LoadImage(1)
    Call LoadImage(2)

End Sub
Private Sub LoadImage(ByVal which As Integer)

End Sub


Private Function PriorityHolder() As String
    PriorityHolder = "L27"
End Function
Private Sub SwitchCards()
    Call SwitchPriority
    Dim Temp As Integer
    Temp = Range("L22").Value
    Range("L22").Value = Range("M22").Value
    Range("M22").Value = Temp
    Call UpdateCardImages


End Sub

Private Function DealerHolder() As String

    DealerHolder = "I1"
End Function
Private Function PlayerArray() As String
    PlayerArray = "K"
End Function

Sub DONOTHINGFFS()
End Sub

1 个答案:

答案 0 :(得分:1)

VBA 结帐错误部分。这将允许您在代码中包含逻辑以捕获错误并允许您分析它们。

如果您发布问题逻辑的一些示例,我们可以帮助您使用错误