+ Reply to Thread
Results 1 to 5 of 5

If Intersect

Hybrid View

  1. #1
    Registered User
    Join Date
    01-28-2010
    Location
    Akron, OH
    MS-Off Ver
    Excel 2007
    Posts
    11

    If Intersect

    Can someone troubleshoot this code for me please?

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B1,B2")) Is Nothing Then
       Exit Sub
     Else
          If Intersect(Target, Range("B1")) Then
            Application.EnableEvents = False
            Range("B2").Formula = "=(B1 * 2)"
            Application.EnableEvents = True
       ElseIf Intersect(Target, Range("B2")) Then
            Application.EnableEvents = False
            Range("B1").Formula = "=(B2 / 2)"
            Application.EnableEvents = True
    
      End If
      End If
    End Sub
    It works when I input into B1, but gives me an error on input of B2.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: If Intersect

    Try this:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("B1:B2")) Is Nothing Then Exit Sub
    
        Application.EnableEvents = False
        
        If Target.Address = "$B$1" Then
            Range("B2").Formula = "=2*B1"
        ElseIf Target.Address = "$B$2" Then
            Range("B1").Formula = "=B2/2"
        End If
        
        Application.EnableEvents = True
    End Sub
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: If Intersect

    Hi Astanix, you might try this instead:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B1,B2")) Is Nothing Then
        Application.EnableEvents = False
        If Target.Address = "$B$1" Then
            Range("B2").Formula = "=B1*2"
        Else
            Range("B1").Formula = "=B2/2"
        End If
        Application.EnableEvents = True
    End If
    End Sub

  4. #4
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: If Intersect

    astanix,

    Try:

    
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B1:B2")) Is Nothing Then Exit Sub
    If Target = "" Then Exit Sub
    If Target.Address = "$B$1" Then
      Application.EnableEvents = False
      Range("B2").Formula = "=(B1 * 2)"
      Application.EnableEvents = True
    ElseIf Target.Address = "$B$2" Then
      Application.EnableEvents = False
      Range("B1").Formula = "=(B2 / 2)"
      Application.EnableEvents = True
    End If
    End Sub
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  5. #5
    Registered User
    Join Date
    01-28-2010
    Location
    Akron, OH
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: If Intersect

    I didn't try them all, just Paul's and it works. I was having trouble figuring out how to do the Target.Address, didn't know about that.
    Thanks a lot to all of you who helped

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1