You are here:

Excel/Excel vba Worksheet_change target as union

Advertisement


Question
Hi,

I have written the following code:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim x As Range
Dim y As Range
Dim UnionRange As Range

On Error GoTo Errhand1

Set x = Range("B2:C7")
Set y = Range("C6:F8")
Set UnionRange = Union(x, y)

If Intersect(Target, UnionRange) Is Nothing Then Exit Sub

With UnionRange

.Formula = "=10"
.Font.Bold = True

End With

Errhand1: Exit Sub

End Sub


The first time i make a change in the target area the code works fine.
The second time it always throws an unhandled win32 Exception and excel closes.
I searched the web for the exception: unhandled win32 exception occurred in EXCEL.EXE [6172]

It suggested making changes to the registry which i will not do.

Is there something wrong with how the code is written? I want the worksheet change event to apply to any cell changed in the UnionRange.

Many thanks,

Quentin

Answer
When you set .Formula = "=10", that triggers the worksheet_change event again, and it goes into a loop. Try this:
Private Sub Worksheet_Change(ByVal Target As Range)

   Dim x As Range
   Dim y As Range
   Dim UnionRange As Range

   On Error GoTo Errhand1

   Set x = Range("B2:C7")
   Set y = Range("C6:F8")
   Set UnionRange = Union(x, y)

   If Intersect(Target, UnionRange) Is Nothing Then Exit Sub
   Application.EnableEvents = False   '<=========won't re-trigger the event
   With UnionRange

       .Formula = "=10"
       .Font.Bold = True

   End With

Errhand1:
   Application.EnableEvents = True '<=========reset

End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.