Thread: error 1004?
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default error 1004?

Your code calls itself--you have range("c6").select. If you didn't start in C6,
then excel sees that .select as a selection change and fires.

Since it's calling itself it runs twice. The second time through the worksheet
is protected. Kablewie!

You could do this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
me.Unprotect Password:="peterke"
Application.ScreenUpdating = False
application.enableevents = false
me.Range("C6").Select
ActiveCell.FormulaR1C1 = "=SUM(R[1]C,1)"
me.Range("C6").Select
me.Protect Password:="peterke"
Application.ScreenUpdating = True
application.enableevents = false
End Sub

(I like to use the me. keyword for stuff behind the worksheet.)

But even better, you don't have to select:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Unprotect Password:="peterke"
Application.ScreenUpdating = False
me.Range("C6").FormulaR1C1 = "=SUM(R[1]C,1)"
me.Protect Password:="peterke"
Application.ScreenUpdating = True
End Sub

Peterke wrote:

Hi,

Why does the error 1004 occure here?
(error marked between <<)
and what do I do to avoid it in the future?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Unprotect Password:="peterke"
Application.ScreenUpdating = False
Range("C6").Select
ActiveCell.FormulaR1C1 = "=SUM(R[1]C,1)"<<

Range("C6").Select
ActiveSheet.Protect Password:="peterke"
Application.ScreenUpdating = True
End Sub

greets,
Peterke


--

Dave Peterson