macro wants to exit excel

C

curlydave

I don't know why but this macro wants to exit excel when I run it

Sub resetpage()
Range("A3:S20").Select
Selection.ClearContents
Range("A3").Select
userform1.Hide


End Sub
I have userform1.hide because I have a worksheetchange event that goes
like this:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Target.Column = 1 Then
userform1.Show False
myRow = Target.Row
End If
End Sub

When I run Sub resetpage()
it clears the contents and then asks if I want to save changes as if I
had clicked exit

Why is this now happening? it doesn't happen all the time ,but it
happens alot
 
D

Dave Peterson

I don't see anything that would make excel want to quit.

But can you disable events before you clear the contents?

Sub resetpage()
Range("A3:S20").Select
application.enableevents = false
Selection.ClearContents
application.enableevents = true
Range("A3").Select
userform1.Hide
End Sub

This'll stop the worksheet_change event from firing. Is that what you want?
 
C

curlydave

Thanks Dave,
The problem seems to be the userform1.hide
I therefore changed the worksheetchangecode to

Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
myRow = Target.Row

If Union(Range("$A3:$A20"), Target).Address = Range("$A3:$A20").Address
Then
If Target = "" Then
UserForm1.Hide
Else: UserForm1.Show False
End If
End If

End Sub
this seems to have solved the problem......
 
D

Dave Peterson

Glad you got it working.
Thanks Dave,
The problem seems to be the userform1.hide
I therefore changed the worksheetchangecode to

Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
myRow = Target.Row

If Union(Range("$A3:$A20"), Target).Address = Range("$A3:$A20").Address
Then
If Target = "" Then
UserForm1.Hide
Else: UserForm1.Show False
End If
End If

End Sub
this seems to have solved the problem......
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top