Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8982129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:30:34+00:00 2026-06-15T20:30:34+00:00

I am having a problem with Excel crashing, when I run VBA code on

  • 0

I am having a problem with Excel crashing, when I run VBA code on an excel sheet.
I’m trying to add the following formula on worksheet change:

Private Sub Worksheet_Change(ByVal Target As Range)
   Worksheets("testpage").Range("A1:A8").Formula = "=B1+C1"
End Sub

When this code is run i get a message saying “excel has encountered a problem and needs to close” and excel closes.

enter image description here

If I run the code in the Worksheet_Activate() procedure, it works fine and doesn’t crash

Private Sub Worksheet_Activate()
   Worksheets("testpage").Range("A1:A8").Formula = "=B1+C1"
End Sub

But I really need it to work in the Worksheet_Change() procedure.

Has anyone experienced similar crashes when using the Worksheet_Change() event and can anyone point in the right direction to fix this issue ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:30:34+00:00Added an answer on June 15, 2026 at 8:30 pm

    I recommend this when using Worksheet_Change

    1. You do not need the sheet name. In a Sheet Code Module, an unqualified Range reference refers to that sheet. That said, it is clearer to use the Me qualifier. If you are trying to use another sheet, then qualify the range reference with that sheet.

    2. Whenever you are working with Worksheet_Change event, always switch Off events if you are writing data to any cell. This is required so that the code doesn’t retrigger the Change event, and go into a possible endless loop

    3. Whenever you are switching off events, use error handling to turn it back on, else if you get an error, the code will not run the next time.

    Try this

    Private Sub Worksheet_Change(ByVal Target As Range)
        On Error GoTo Whoa
        
        Application.EnableEvents = False
        
        Me.Range("A1:A8").Formula = "=B1+C1"
        
    Letscontinue:
        Application.EnableEvents = True
        Exit Sub
    Whoa:
        MsgBox Err.Description
        Resume Letscontinue
    End Sub
    

    Few other things that you may want to know when working with this event.

    If you want to ensure that the code doesn’t run when more than one cell is changed then add a small check

    Private Sub Worksheet_Change(ByVal Target As Range)
        '~~> For Excel 2003
        If Target.Cells.Count > 1 Then Exit Sub
        
        '
        '~~> Rest of code
        '
    End Sub
    

    The CountLarge was introduced in Excel 2007 onward because Target.Cells.Count returns an Long value which can error out in Excel 2007 becuase of increased total cells count.

    Private Sub Worksheet_Change(ByVal Target As Range)
        '~~> For Excel 2007
        If Target.Cells.CountLarge > 1 Then Exit Sub
        '
        '~~> Rest of code
        '
    End Sub
    

    To work with all the cells that were changed use this code

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim aCell As Range
        
        For Each aCell In Target.Cells
            With aCell
                '~~> Do Something
            End With
        Next
    End Sub
    

    To detect change in a particular cell, use Intersect. For example, if a change happens in Cell A1, then the below code will fire

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Me.Range("A1")) Is Nothing Then
            MsgBox "Cell A1 was changed"
            '~~> Your code here
        End If
    End Sub
    

    To detect change in a particular set of range, use Intersect again. For example, if a change happens in range A1:A10, then the below code will fire

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Me.Range("A1:A10")) Is Nothing Then
            MsgBox "one or more Cells in A1:A10 range was changed"
            '~~> Your code here
        End If
    End Sub
    

    Note: If you were getting an error earlier and you made the above changes and If your code is still not working then it is possible that the events have not been reset. In the Immediate Window, type Application.EnableEvents = True and press the ENTER key. This will reset it to True. If you do not see the Immediate Window, the press the shortcut key Ctl+G to launch the Immediate Window.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with some excel code that I am having trouble getting
I'm having a problem with the conversion to Excel code I'm finding. I am
I am having problem with excel formula bar when formula exceeds 1024 characters. It
I am Having a problem here.I am downloading the excel sheet using Url and
I am having a problem with reading DateColumns from an excel sheet. Sometimes people
I'm trying to export my GridView to excel, but I'm having some problems. At
I'm having problem with datagrid view. I have attached an image with the code
I am having problem with drawing multiple lines during repaint. The code is as
I am having an problem, while java application reading Excel file .xlsx extention, The
I am having a problem programmatically exporting Excel sheets that contain sparklines to PDF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.