Share via

My Excel VBA code stopped working from one day to the next?

Anonymous
2024-08-06T04:00:24+00:00

I have the following code set up in a worksheet (actually on two worksheets in the same file). The first part is to move lines from one sheet to another based on changing a drop-down selection, the second part is to allow for multiple selections in a drop-down list (in a different cell to the first part).

The code was set up yesterday and it worked fine, but today it isn't working. I haven't changed the code itself since then, and I've tried closing/opening the file again but still isn't working.

Any ideas on why it suddenly stopped functioning? Thank you!

Private Sub Worksheet_Change(ByVal Target As Range)

Worksheet_Change_A Target

Worksheet_Change_B Target

End Sub

Private Sub Worksheet_Change_A(ByVal Target As Range)

    If Target.CountLarge > 1 Then Exit Sub

    If Target.Column = 7 And Target.Row > 2 And Target.Value = "Complete" Then

        Application.EnableEvents = False

        Range(Cells(Target.Row, "A"), Cells(Target.Row, "L")).Copy Sheets("Completed Projects").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)

        Rows(Target.Row).Delete

        Application.EnableEvents = True

    End If

End Sub

Private Sub Worksheet_Change_B(ByVal Target As Range)

Dim Oldvalue As String

Dim Newvalue As String

Application.EnableEvents = True

On Error GoTo Exitsub

If Target.Column = 5 Then

  If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then

    GoTo Exitsub

  Else: If Target.Value = "" Then GoTo Exitsub Else

    Application.EnableEvents = False

    Newvalue = Target.Value

    Application.Undo

    Oldvalue = Target.Value

      If Oldvalue = "" Then

        Target.Value = Newvalue

      Else

        If InStr(1, Oldvalue, Newvalue) = 0 Then

            Target.Value = Oldvalue & vbNewLine & Newvalue

      Else:

        Target.Value = Oldvalue

      End If

    End If

  End If

End If

Application.EnableEvents = True

Exitsub:

Application.EnableEvents = True

End Sub

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-08-06T23:06:35+00:00

    Hi - it sort of works but prompts two different error messages.

    It allows adding multiple entries from the drop-down list in column E, but if you try to delete one of them it brings up an error message - Run-time error '1004': Method 'Undo' of object '_Application' failed. Debugging highlights the 'Application.Undo' line in the code.

    Then for the drop down list in G which is the one that moves lines between sheets, the line moves but it brings up an error message - Run-time error '424': Object required. Debugging highlights the 'If Target.Column = 5 Then' line in the code

    Any idea on how to fix that?

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.6K Reputation points
    2024-08-06T08:11:57+00:00

    Activate the Visual Basic Editor.

    Press Ctrl+G to activate the Immediate window.

    Type or copy/paste

    Application.EnableEvents = True

    Press Enter.

    Switch back to Excel.

    Does the code work again?

    Was this answer helpful?

    0 comments No comments