sirpelidor


Hi, I wrote some vba code within the Worksheet_Change sub. It supposedly fires whenever a cell value has been changed.

Problem is...sometimes it fires, sometime it doesn't. Whenever it doesn't (probably because it encounted bug within my code), no matter what I do, I can't re-enable the marco again.

So far, the only way for me to go around that copy my code into a clipboard, close out excel (not save). Re-open excel and paste my code back in. I'm sure there must be ways I can go around that without having to do what I've been doing ( tried hitting the square button but still doesn't work).

How can I re-enable marco after it stopped

Thank you.



Re: how do re-enable marco after it stopped?

Andy Pope


Hi,

A possible problem maybe you are disabling events at the start of your macro and then not turning them back on again.
Do you use something like this

Application.EnableEvents = False

It would really help is we could see your code.








Re: how do re-enable marco after it stopped?

sirpelidor

Hi Andy,

I think you hit the nail by the head. I do have Application.EnableEvent = False within my code. Like you suggested, it most lilkely encountered bugs (as i modify my code) after Event turned to false and I have don't know how to turn it back on.

[code]

'run this method when cell value has changed
Private Sub Worksheet_Change(ByVal Target As Range)

'only excute this method when cell is D4
If Target.Address = "$D$4" And Target.Count = 1 Then
Dim orgStr As String, storeStr As String
orgStr = Target.Value 'this will store the value user just changed
Application.EnableEvents = False

'set a cell from datasource as a place holder
Dim rng As Range
Set rng = Worksheets("datasource").Cells(Target.Row, Target.Column)
storeStr = rng.Value

Target.Value = Trim(Target.Value)
If Target.Value = "" Then 'user deleted value
rng.Value = ""
ElseIf manualDelete(orgStr, storeStr) Then 'accept/retry datay entry if user manually edit data
Dim userResponse As Integer, promptMsg As String
promptMsg = "You have manually edited the value. Excel cannot gurantee the data intgrity. Please select 'OK' to accept manual changes or 'Cancel' to re-enter the value."
'1 is ok, 2 is cancel
userResponse = MsgBox(promptMsg, vbOKCancel, "Attention when enter data manually:")
If userResponse = 1 Then
rng.Value = Target.Value
Else
Target.Value = rng.Value
End If
Else 'user used drop-down-list
If rng.Value = "" Then 'brand new entry
rng.Value = Target.Value
Else
If Not isItemExit(Target.Value, rng) Then 'write to binder only when value is unique
rng = rng & ", " & Target.Value
End If
Target.Value = rng.Value
End If
End If 'target.value = ""

Application.EnableEvents = True
End If 'end cell is D4
End Sub

[/code]





Re: how do re-enable marco after it stopped?

Andy Pope

Use some error trapping code.

Private Sub Worksheet_Change(ByVal Target As Range)

'only excute this method when cell is D4
If Target.Address = "$D$4" And Target.Count = 1 Then
Dim orgStr As String, storeStr As String
orgStr = Target.Value 'this will store the value user just changed

On Error GoTo ErrWorksheetChange
Application.EnableEvents = False

'set a cell from datasource as a place holder
Dim rng As Range
Set rng = Worksheets("datasource").Cells(Target.Row, Target.Column)
storeStr = rng.Value

Target.Value = Trim(Target.Value)
If Target.Value = "" Then 'user deleted value
rng.Value = ""
ElseIf manualDelete(orgStr, storeStr) Then 'accept/retry datay entry if user manually edit data
Dim userResponse As Integer, promptMsg As String
promptMsg = "You have manually edited the value. Excel cannot gurantee the data intgrity. Please select 'OK' to accept manual changes or 'Cancel' to re-enter the value."
'1 is ok, 2 is cancel
userResponse = MsgBox(promptMsg, vbOKCancel, "Attention when enter data manually:")
If userResponse = 1 Then
rng.Value = Target.Value
Else
Target.Value = rng.Value
End If
Else 'user used drop-down-list
If rng.Value = "" Then 'brand new entry
rng.Value = Target.Value
Else
If Not isItemExit(Target.Value, rng) Then 'write to binder only when value is unique
rng = rng & ", " & Target.Value
End If
Target.Value = rng.Value
End If
End If 'target.value = ""

End If 'end cell is D4

ErrWorksheetChange:
Application.EnableEvents = True
Exit Sub

End Sub