+ Reply to Thread
Results 1 to 2 of 2

How to Merge Two Worksheet_Change Codes

Hybrid View

  1. #1
    Registered User
    Join Date
    06-06-2016
    Location
    istanbul
    MS-Off Ver
    2013
    Posts
    1

    How to Merge Two Worksheet_Change Codes

    Hi,
    I am trying to merge two VBA codes;

    Code1:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 4 Then
    Target.Offset(0, 1).ClearContents
    Target.Offset(0, 2).ClearContents
    Target.Offset(0, 3).ClearContents
    Target.Offset(0, 4).ClearContents
    Target.Offset(0, 5).ClearContents
    Target.Offset(0, 6).ClearContents
    Target.Offset(0, 7).ClearContents
    Target.Offset(0, 8).ClearContents
    Target.Offset(0, 9).ClearContents
    Target.Offset(0, 10).ClearContents
    Target.Offset(0, 11).ClearContents
    End If
    End Sub


    Code2:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Set xxx = Intersect(Target, Range("E2:O10000")) 
    If Not xxx Is Nothing Then
    If HasValidation(xxx) Then
    Exit Sub
    Else
    Application.EnableEvents = False
    Application.Undo
    Application.EnableEvents = True
    MsgBox "Your last operation was cancelled. It would have deleted data validation rules.", vbCritical
    End If
    End If
    End Sub
    
    
    Private Function HasValidation(r) As Boolean
    HasValidation = True
    'Returns True if every cell in Range r uses Data Validation
    On Error Resume Next
    For Each cll In r.Cells
    x = cll.Validation.Type
    If Err.Number <> 0 Then
    HasValidation = False
    Exit For
    End If
    Next cll
    End Function
    I have asked this other forums and someone send me below code. It works fine for 2 times but then it shows error on Application.Undo.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Application.EnableEvents = False
        If Target.Column = 6 Then
            Range(Target.Offset(0, 1).Address, Target.Offset(0, 11).Address).ClearContents
        End If
        Set xxx = Intersect(Target, Range("E2:F32,H2:H32"))
        If Not xxx Is Nothing Then
            If HasValidation(xxx) Then
                Application.EnableEvents = True
                Exit Sub
            Else
                Application.Undo
                MsgBox "Your last operation was cancelled. It would have deleted data validation rules.", vbCritical
            End If
        End If
        Application.EnableEvents = True
    End Sub
    
    Private Function HasValidation(r) As Boolean
        HasValidation = True
        On Error Resume Next
        For Each cll In r.Cells
            x = cll.Validation.Type
            If Err.Number <> 0 Then
                HasValidation = False
                Exit For
            End If
        Next cll
    End Function

    It looks like i have to change something but i couldn't find. Is there anyone to explain?

    Thanks!
    Last edited by merveileuse; 07-09-2019 at 10:56 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to Merge Two Worksheet_Change Codes

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    In addition please upload the workbook so we may see the request in context.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ 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. Merging 2 Private Sub Worksheet_Change codes
    By anilpatni1234 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-01-2019, 10:08 PM
  2. How to compile two codes in Private Sub Worksheet_Change(ByVal Target As Range)
    By rajeev.raj in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-19-2019, 01:51 PM
  3. Combine Two Private Sub Worksheet_Change codes in same worksheet
    By abhinavbinkar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-01-2019, 06:58 AM
  4. [SOLVED] Combine two Private Sub Worksheet_Change VBA codes
    By sloshpuppy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-13-2017, 04:55 AM
  5. Connecting two VBA codes (Private Sub Worksheet_Change(ByVal Target As Range)
    By TinRu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-26-2014, 06:47 AM
  6. Combine two Worksheet_Change Codes
    By otherbobby in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-26-2014, 04:10 PM
  7. [SOLVED] merge with Word How can I get excel to mail merge zip codes plus 4 correctly?
    By Kathy at Sauder Feeds in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-30-2005, 07:05 AM

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