Worksheet Event Code

ayazgreat

Well-known Member
Joined
Jan 19, 2008
Messages
1,151
Hi

I have following codes, there is a problem in these codes, when I write mistakenly any word or number instead of D or d in target column then macro stops to work even I write again D or d then macro does not work and I have close workbook and reopen it and write correct word to get the macro worked. Could anybody give me some help to correct it ?

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
      
    If Target.Column = 11 Then
        Application.EnableEvents = False
        If Target.Value = "D" Or Target.Value = "d" Then
            Target.EntireRow.Copy
            Worksheets("Issued Record").Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteAll
            Target.EntireRow.Delete
            Application.EnableEvents = True
             
            Exit Sub
        End If
    End If
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Just change where you disable events

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Column = 11 Then
        If Target.Value = "D" Or Target.Value = "d" Then
            Application.EnableEvents = False
            Target.EntireRow.Copy
            Worksheets("Issued Record").Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteAll
            Target.EntireRow.Delete
            Application.EnableEvents = True
            Exit Sub
        End If
    End If
End Sub
 
Upvote 0
Sorry I do not understand what you mean
Look at this part of your code...
Code:
    If Target.Column = 11 Then
        [COLOR=red]Application.EnableEvents = False[/COLOR]
        If Target.Value = "D" Or Target.Value = "d" Then
            Target.EntireRow.Copy
            Worksheets("Issued Record").Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteAll
            Target.EntireRow.Delete
            [COLOR=red]Application.EnableEvents = True[/COLOR]
            Exit Sub
        End If
The Application.EnableEvents=False statement disables events BEFORE you test for the Target.Value equalling "D" or "d"; HOWEVER, you only execute the Application.EnableEvents=True statement IF Target.Value equals "D" or "d'. So, if it doesn't equal "D" or "d", the events remain turned off.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

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