Intersect multiple non contiguous ranges

PATSYS50

New Member
Joined
May 9, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi all, vba beginner here.

The below code is supposed to fill up cell F10 with the value when D10 is entered and vice versa.

But is is giving out error. Can someone point to me what is wrong?

Thanks in advance.

VBA Code:
Private Sub worksheet_change(ByVal target As Range)

If Not Intersect(target, Union(Range("D10"), Range("F10"))) Is Nothing Then

Range("D10").Value = (Range("F10").Value - 32) / 1.8
Range("F10").Value = Range("D10").Value * 1.8 + 32

End If

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Because, when you set a cell value, it triggers a Worksheet_Change event again. And because the changed cell intersects with the target, it fires again and again. One solution will be disabling events for a while.

VBA Code:
Private Sub worksheet_change(ByVal target As Range)

  If Not Intersect(target, Union(Range("D10"), Range("F10"))) Is Nothing Then
    Application.EnableEvents = False
    Range("D10").Value = (Range("F10").Value - 32) / 1.8
    Range("F10").Value = Range("D10").Value * 1.8 + 32
    Application.EnableEvents = True
  End If

End Sub
 
Upvote 0
Solution
Because, when you set a cell value, it triggers a Worksheet_Change event again. And because the changed cell intersects with the target, it fires again and again. One solution will be disabling events for a while.

VBA Code:
Private Sub worksheet_change(ByVal target As Range)

  If Not Intersect(target, Union(Range("D10"), Range("F10"))) Is Nothing Then
    Application.EnableEvents = False
    Range("D10").Value = (Range("F10").Value - 32) / 1.8
    Range("F10").Value = Range("D10").Value * 1.8 + 32
    Application.EnableEvents = True
  End If

End Sub
Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,187
Messages
6,123,540
Members
449,107
Latest member
caya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top