VBA Macro Repeats and Will Not Stop

Jonathan Challinor 96 Reputation points
2021-01-09T14:51:28.78+00:00

Hi All,

My VBA code will not stop. My spreadsheet is being fed by an API. The code is design to trigger on the value of a cell. Before the value to check the code asks if another value is 1, this cell value is inputted by the code and if it equals 1 it stops the code from executing.

The code checks 3 different values, The value of 1 never appears so the code continues to cut and paste. I am sure I have something mixed up or wrong but I am not getting any compile errors or debug so the code looks correct just maybe not in the correct order. I stop the code with application events if the value is 1, problem is the value never becomes 1. I have change around the TRU and False order but it does not work either way. Code below

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet: Set ws = Sheet1
If ws.Range("BM1").Value <> 1 Then
Application.EnableEvents = True
End If
If ws.Range("BK1").Value = 10 Then
With Range("BC10:BC68").Copy
Range("BC10").PasteSpecial Paste:=xlPasteValues
Range("BM1") = 1
End With
Range("BM1").Value = 1
Application.EnableEvents = False
End If
If ws.Range("BM2").Value <> 1 Then
Application.EnableEvents = True
End If
If ws.Range("BK2").Value = 5 Then
With Range("BD10:BD68").Copy
Range("BD10").PasteSpecial Paste:=xlPasteValues
Range("BM2") = 1
End With
Range("BM2").Value = 1
Application.EnableEvents = False
End If
If ws.Range("BM3").Value <> 1 Then
Application.EnableEvents = True
End If
If ws.Range("BK3").Value = 105 Then
With Range("BE10:BE68").Copy
Range("BE10").PasteSpecial Paste:=xlPasteValues
Range("BM3") = 1
End With
Range("BM3").Value = 1
Application.EnableEvents = False
End If
End Sub

Many Thanks

Jonathan

Office Visual Basic for Applications
Office Visual Basic for Applications
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Visual Basic for Applications: An implementation of Visual Basic that is built into Microsoft products.
1,496 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jonathan Challinor 96 Reputation points
    2021-01-09T15:42:25.85+00:00

    Sorry, just a correction, it is not a macro it is VBA code, thanks

    0 comments No comments

13 additional answers

Sort by: Most helpful
  1. HansV 966 Reputation points MVP
    2021-01-09T15:38:18.883+00:00

    The main problem is that you have to set Application.EnableEvents to False before modifying cells, then to True afterwards, instead of the other way round.
    Apart from that, it's not entirely clear what you want to react to. Does this do what you want?

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Range("BM1"), Target) Is Nothing Then
            If Range("BM1").Value <> 1 And Range("BK1").Value = 10 Then
                Application.EnableEvents = False
                Range("BC10:BC68").Copy
                Range("BC10").PasteSpecial Paste:=xlPasteValues
                Range("BM1").Value = 1
            End If
        End If
        If Not Intersect(Range("BM2"), Target) Is Nothing Then
            If Range("BM2").Value <> 1 And Range("BK2").Value = 5 Then
                Application.EnableEvents = False
                Range("BD10:BD68").Copy
                Range("BD10").PasteSpecial Paste:=xlPasteValues
                Range("BM2").Value = 1
                Application.EnableEvents = True
            End If
        End If
        If Not Intersect(Range("BM3"), Target) Is Nothing Then
            If Range("BM3").Value <> 1 And Range("BK").Value = 105 Then
                Application.EnableEvents = False
                Range("BE10:BE68").Copy
                Range("BE10").PasteSpecial Paste:=xlPasteValues
                Range("BM3").Value = 1
                Application.EnableEvents = True
            End If
        End If
    End Sub
    

  2. Jonathan Challinor 96 Reputation points
    2021-01-09T16:02:02.48+00:00

    Hans,

    Just to add, I have multiple sheets so need to be able to repeat to procure for each of my multiple sheets

    Thanks

    Jonathan

    0 comments No comments

  3. HansV 966 Reputation points MVP
    2021-01-09T16:43:23.497+00:00

    Should the code be identical for all sheets?
    Do all sheets need to look at BKn and BMn on their own sheet or on Sheet1?

    0 comments No comments

  4. Jonathan Challinor 96 Reputation points
    2021-01-09T16:58:48.243+00:00

    Each sheet is identical, 40 different sheets for sheet 1 to 40, each sheet has a separate set of data. All sheets look at BKn and BMn

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.