+ Reply to Thread
Results 1 to 10 of 10

Worksheet Change Event Non Contiguous Range

Hybrid View

  1. #1
    Registered User
    Join Date
    08-18-2020
    Location
    Kuala Lumpur
    MS-Off Ver
    MS Office 2019
    Posts
    28

    Worksheet Change Event Non Contiguous Range

    Hey guys, I am trying to write for a worksheet change event for two non contiguous range , which are cells A&lastrow and C&lastrow. For example, if lastrow=5, then it is A5, and C5, while excluding B5. This is my code, and it is not working, any idea on how to fix the syntax.


    Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lastrow As Long
    lastrow = 5


    If Not Intersect(Target, Range("A & lastrow, C & lastrow ")) Is Nothing Then
    MsgBox "Hello"

    End If
    End Sub

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    13,000

    Re: Worksheet Change Event Non Contiguous Range

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim LastRow As Long
        Dim RngA    As String
        Dim RngC    As String
        LastRow = 5
        RngA = "A1:A" & LastRow
        RngC = "C1:C" & LastRow
        
        If Not Intersect(Target, Range(RngA, RngC)) Is Nothing Then
            MsgBox "Hello"
        Else
            MsgBox "bye"
        End If
    End Sub
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    08-18-2020
    Location
    Kuala Lumpur
    MS-Off Ver
    MS Office 2019
    Posts
    28

    Re: Worksheet Change Event Non Contiguous Range

    Doesn't work. This is non contiguous range ,meaning B5 is to be excluded, your codes identify B5 as well .

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Worksheet Change Event Non Contiguous Range

    Does this do what you want?

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim targetArea As Range
        Dim intersectCount As Long
        Dim testRange As Range
        Dim testArea As Range
        Dim lastRow As Long
        
        lastRow = 5
        Set testRange = Union(Range("A" & lastRow), Range("C" & lastRow))
        For Each targetArea In Target.Areas
            For Each testArea In testRange.Areas
                If Not Intersect(targetArea, testArea) Is Nothing Then
                    intersectCount = intersectCount + 1
                    Exit For
                End If
            Next
        Next
        If testRange.Areas.Count = Target.Areas.Count Then
            If intersectCount = testRange.Areas.Count Then
                MsgBox "Matched"
            Else
                MsgBox "No Match"
            End If
        Else
            MsgBox "No Match"
        End If
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    08-18-2020
    Location
    Kuala Lumpur
    MS-Off Ver
    MS Office 2019
    Posts
    28

    Re: Worksheet Change Event Non Contiguous Range

    Hi Andy, nope doesn't solve it.

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Worksheet Change Event Non Contiguous Range

    You need to provide more useful feed back.

    For me, if I select A5 and C5 and enter values in both cells at once using CTRL+ENTER it works.

    If I have both selected but only enter value into 1 cell, using Enter, then it does not work.
    If I select only 1 of the cells and enter it does not work.
    If I select more cells it does not work.

    Please explain the circumstances under which it should work

  7. #7
    Registered User
    Join Date
    08-18-2020
    Location
    Kuala Lumpur
    MS-Off Ver
    MS Office 2019
    Posts
    28

    Re: Worksheet Change Event Non Contiguous Range

    Andy, I have made it very clear on my question. Non Contiguous. Should be understood that one of either A5 or C5 will be inputted, the worksheet change event will be fired.

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Worksheet Change Event Non Contiguous Range

    Maybe it is just a very simple case, like :

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim lastrow As Long
      lastrow = 5
    
      If Not Intersect(Target, Union(Range("A" & lastrow), Range("C" & lastrow))) Is Nothing Then
         MsgBox "Hello"
      End If
    End Sub
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Worksheet Change Event Non Contiguous Range

    To me, non contiguous means a reference to 2 or more cells.

    You simply mean 2 non adjacent cells

  10. #10
    Registered User
    Join Date
    08-18-2020
    Location
    Kuala Lumpur
    MS-Off Ver
    MS Office 2019
    Posts
    28

    Re: Worksheet Change Event Non Contiguous Range

    yes Andy.

    I have found the solution . Its as follows

    if not intersect(target,union(range("A" & lastrow),range("D"&lastrow))) is nothing then msgbox "hello"

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Execute Worksheet change event only within specific range
    By ABSTRAKTUS in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-16-2020, 11:31 AM
  2. [SOLVED] Worksheet change event if in range negative value find then higlight the value
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-20-2017, 05:24 AM
  3. [SOLVED] Worksheet change event to move values up over non-contiguous ranges (remove spaces)
    By L plates in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2016, 11:57 AM
  4. How to make a Worksheet Calculate event when a range of cells change?
    By Invicta084 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-15-2014, 07:20 PM
  5. [SOLVED] need help setting range in worksheet change event
    By ShoshanaM in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2012, 04:07 PM
  6. Worksheet Change Event-change the range in my VBA
    By systemx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-27-2006, 05:00 AM
  7. [SOLVED] Worksheet Change Event-when a cell with a certain range of cells) are changed?
    By Steph in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-25-2005, 07:06 PM

Tags for this Thread

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