+ Reply to Thread
Results 1 to 19 of 19

Ensuring event does not fire when inserting/deleting rows

Hybrid View

  1. #1
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Ensuring event does not fire when inserting/deleting rows

    Hi All

    I have the below code which works when trying to enter a specific text in cell...Problem is it errors when I try to insert rows prior to typing in new info...Also crashes when trying to delete rows.
    Is there a workaround to have event not fire when inserting or deleting rows...Other than "On error"


    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("A7:A5000")) Is Nothing Then
            If Left(Target.Value, 2) <> "DP" Then
                Application.EnableEvents = False
                MsgBox "PM NUMBER MUST START WITH DP", vbInformation, ""
                Target = ""
                Target.Activate
                Application.EnableEvents = True
            End If
        End If
    End Sub
    Last edited by Sintek; 05-03-2018 at 12:41 PM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,438

    Re: Ensuring event does not fire when inserting/deleting rows

    I don't think you can separately block "insert and delete" from the other actions that trigger the change event. I think you need to investigate what is different about Target when a row is inserted/deleted from when a single cell is entered. For testing, I added a Stop statement to the top of the procedure. When it entered debug mode, I checked the address of target when I inserted a row. Target was the entire new, empty row. Of course, the Left() function does not know how to extract the left two characters from an entire empty row, so it sets an error. Your main test here seems to be about editing/entering a single cell, so maybe a test for If Target.count>1 would be sufficient. Or test If Target.cells(1,1) is empty.

    Is there more to this change procedure than you are showing here. In a completely different approach, why not use Excel's built in data validation to restrict entry to text beginning with DP? https://support.office.com/en-us/art...6-eff3ce5f7249 The help file includes an example "custom formula" data validation for "entry must begin with 'ID' and be more than 9 characters long", which should be easily adapted to "entry must begin with 'DP'".
    Last edited by MrShorty; 05-03-2018 at 01:09 PM.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Ensuring event does not fire when inserting/deleting rows

    IF we can assume that an entire row won't be copy/pasted then start the code with:

    If Target.Columns.Count <> Columns.Count Then Exit Sub
    Again, this will still prevent the macro from firing if a full row is copy/pasted.

    For dealing with an Insert only, you could simply, directly after the If Not Intersect line put

    If Target.Value = vbNullString then Exit Sub.
    This would also prevent the messagebox if the user was clearing a part number.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Ensuring event does not fire when inserting/deleting rows

    Hey there guys...

    This solves...
    If Target.Columns.Count = Columns.Count Then Exit Sub
    That part lol, but now when my insert code runs...back to square 1

    Sub Copy()
    Sheet2.Range("A1:D1").Copy
    Sheet1.Range("A9").PasteSpecial xlPasteValues
    End Sub
    Last edited by Sintek; 05-03-2018 at 02:01 PM.

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Ensuring event does not fire when inserting/deleting rows

    Hi Guys...

    Thanks for responses...The only manual intervention will be insert row...Nothing else
    Another macro will filter and delete rows later.
    Edit...
    Have tried all of above and none remedy the situation...Am I missing something...
    Last edited by Sintek; 05-03-2018 at 01:37 PM.

  6. #6
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Ensuring event does not fire when inserting/deleting rows

    Naand sintek,

    Hier's hy, Vat hom!

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lr As Long
    
    lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
        
        If Not Intersect(Target, Range("A7:A" & lr)) Is Nothing Then
            If Left(Target.Value, 2) <> "DP" Then
                Application.EnableEvents = False
                MsgBox "PM NUMBER MUST START WITH DP", vbInformation, ""
                Target = ""
                Target.Activate
           
                Application.EnableEvents = True
            End If
        End If
    End Sub
    Enjoy!
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  7. #7
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Ensuring event does not fire when inserting/deleting rows

    Hey Tjaart...raas jou koppie vanaand...lol

    No diffs...you just added a lrow...

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lr As Long
    
    lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
        
        If Not Intersect(Target, Range("A7:A" & lr)) Is Nothing Then
            If Left(Target.Value, 2) <> "DP" Then
                Application.EnableEvents = False
                MsgBox "PM NUMBER MUST START WITH DP", vbInformation, ""
                Target = ""
                Target.Activate
           
                Application.EnableEvents = True
            End If
        End If
    End Sub
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("A7:A5000")) Is Nothing Then
            If Left(Target.Value, 2) <> "DP" Then
                Application.EnableEvents = False
                MsgBox "PM NUMBER MUST START WITH DP", vbInformation, ""
                Target = ""
                Target.Activate
                Application.EnableEvents = True
            End If
        End If
    End Sub
    Edit...
    As per post 5...this solves the insert and delete rows and the specific text insert...
    If Target.Columns.Count = Columns.Count Then Exit Sub
    Last edited by Sintek; 05-03-2018 at 02:38 PM.

  8. #8
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Ensuring event does not fire when inserting/deleting rows

    Hier is my bewys my maat,

    Please try the attached Workbook any way you wish.

    Regards.
    Attached Files Attached Files
    Last edited by Winon; 05-03-2018 at 03:09 PM. Reason: Posted correct sample Workbook

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Ensuring event does not fire when inserting/deleting rows

    Winon...Your code does what mine in Post1 does...that was not my problem...
    When inserting or deleting a row was my issue...your worksheet fails when doing so...
    Runtime error 13 Type mismatch...

    Hence this line solves that....As per post5.
    If Target.Columns.Count = Columns.Count Then Exit Sub
    What I need is for the code to be altered to allow for my insert macro to work as mentioned in post5...
    Sub Copy()
    Sheet2.Range("A1:D1").Copy
    Sheet1.Range("A9").PasteSpecial xlPasteValues
    End Sub

  10. #10
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Ensuring event does not fire when inserting/deleting rows

    Hi sintek,

    I am confused because it works on my side without any problems. Inserting or deleting lines, copy and paste values, you name it.

    Sorry for wasting your time then.

    Lekker slaap!

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Ensuring event does not fire when inserting/deleting rows

    nee f.k..Ek is ook confused...Tried code on all 3 pc,s and still errors...
    Never wasted time...You got me started way back then on this stuff...

  12. #12
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,438

    Re: Ensuring event does not fire when inserting/deleting rows

    As I noted in my reply, the "type mismatch" error is because the Left() function requires a string input. Target is a range, and, when Target is a multi-cell range, Left() doesn't know what to do with the resulting array of values/empty.

    In addition to the suggestions above, I could see trying something like Left(Target.Cells(1,1).Value,2), which will always pass a single value/empty to the Left() function. That should get rid of the type mismatch error, though I cannot be sure if that represents the desired programming logic when you are inserting rows or pasting multi-cell ranges.

  13. #13
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Ensuring event does not fire when inserting/deleting rows

    @ MrShorty,

    Did my sample Workbook also erred on your side?

    Regards.

  14. #14
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,438

    Re: Ensuring event does not fire when inserting/deleting rows

    @ Winon: Yes, I got a "type mismatch" error when pasting multiple cells into a row starting in column A.

  15. #15
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Ensuring event does not fire when inserting/deleting rows

    @ MrShorty,

    Blast you MrShorty, nice catch. I never tested that.

    The "Fix" maybe?

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lr As Long
    
    lr = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
        
        If Target.Count > 1 Then Exit Sub
        If Not Intersect(Target, Range("A7:A" & lr)) Is Nothing Then
            If Left(Target.Value, 2) <> "DP" Then
                Application.EnableEvents = False
                MsgBox "PM NUMBER MUST START WITH DP", vbInformation, ""
                Target = ""
                Target.Activate
           
                Application.EnableEvents = True
            End If
        End If
    End Sub
    Best Regards.

  16. #16
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Ensuring event does not fire when inserting/deleting rows

    Hi Guys...

    Darn man Winon...Now your code works...strange though cause MrShorty suggested the code in Post 2...When trying it last night it did not work...This morning it does

    Anyway, thank you both for sticking by me...I am a happy chappy
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
         If Not Intersect(Target, Range("A7:A" & Cells(Rows.Count, "A").End(xlUp).Row)) Is Nothing Then
            'If Target.Columns.Count = Columns.Count Then Exit Sub
            If Target.Count > 1 Then Exit Sub
            If Left(Target.Value, 2) <> "DP" Then
                Application.EnableEvents = False
                MsgBox "PM NUMBER MUST START WITH DP", vbInformation, ""
                Target = ""
                Target.Activate
                Application.EnableEvents = True
            End If
        End If
    End Sub

  17. #17
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Ensuring event does not fire when inserting/deleting rows

    Phew, what a relief!

    Thank you sintek, I was at my wits end as to why it did not work?

    Glad it is "Solved" now, and thank you for the Rep+. I appreciate it!

    Mag julle goeie reen ontvang.

  18. #18
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Ensuring event does not fire when inserting/deleting rows

    Hey sintek,

    And yet the thlot pickens.

    The Code fires from Row 1 through Row 6 as well, which it should not do.

    Solution, tweaking your Code;

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
         
        If Not Intersect(Target, Range("A1:A6")) Is Nothing Then Exit Sub
         If Not Intersect(Target, Range("A7:A" & Cells(Rows.Count, "A").End(xlUp).Row)) Is Nothing Then
            'If Target.Columns.Count = Columns.Count Then Exit Sub
            If Target.Count > 1 Then Exit Sub
            If Left(Target.Value, 2) <> "DP" Then
                Application.EnableEvents = False
                MsgBox "PM NUMBER MUST START WITH DP", vbInformation, ""
                Target = ""
                Target.Activate
                Application.EnableEvents = True
            End If
        End If
    
    End Sub
    Geniet!

  19. #19
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Ensuring event does not fire when inserting/deleting rows

    Thanks Winon...Perfection

+ 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] Using .SetFocus inside a TextBox_Exit event causes _Exit event to fire twice.
    By Fattyfatfat Kid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-29-2015, 07:21 PM
  2. [SOLVED] Open Event to Fire During Specified Hour Only
    By dreicer_Jarr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-30-2013, 05:11 AM
  3. can't get calculate event to fire vba
    By Cicada in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-18-2010, 06:47 AM
  4. Deleting and inserting rows
    By blurtoad in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-02-2007, 04:54 AM
  5. Event doesn't fire
    By Frank Xia in forum Excel General
    Replies: 6
    Last Post: 02-10-2006, 09:00 PM
  6. Fire Event only when Cell Change?
    By HotRod in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-28-2005, 04:06 PM
  7. Inserting Rows during a SheetChange event
    By Nirmal Singh in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-07-2005, 10:06 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