View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Stephanie S Stephanie S is offline
external usenet poster
 
Posts: 1
Default error in my VBA (newbie)

I do not normally work in Excel. We (the IT dept at my company) have an
Excel workbook that we inherited from... somewhere. Someone asked me to do
one simple thing. I did it. The problem is I did it on a protected sheet.
While working on the functionality they wanted, I simply unprotected the
sheet so I could change it. And now I am trying to get it to work with the
sheet protected.

I have 2 locked cells, that need to be locked, which is why I need to
protect the sheet.

OK, so I have a button which does this:

Range("D14") = GetTotalInterest(Range("D6"), Range("D10"), Range("D12"),
Range("D8"))

Where GetTotalInterest is a function which returns a double. D14 is one of
the "locked" cells because the folks are not supposed to be able to type
into it. So I need to protect the sheet, which will cause me not to be able
to do

Range("D14") =

Fine. So I try this instead:

Sheets("Variable").Unprotect "pwd"
Range("D14") = GetTotalInterest(Range("D6"), Range("D10"), Range("D12"),
Range("D8"))
Sheets("Variable").Protect "pwd"

Where "pwd" is a not terribly necessary password. The person who created the
sheet, protected with a password. So ... I thought I would continue that
trend, even though it is not terribly important to do so. I get the error:

Unprotect method of Worksheet class failed (number 1004).

I do not know why. I am supplying the correct password in the correct case.
The help on the error message is not helpful.

Anyone have any ideas what I am doing wrong?

Included here is the code for GetTotalInterest. It has an unprotect and
protect in it. So I note it in case it helps.

Function GetTotalInterest(LoanAmt As Double, PayAmt As Double, ActualTerm As
Double, InterestRate As Double) As Double

Dim totalinterest As Variant

Dim e10, e11, e12, e13, e17

' Application.ScreenUpdating = False
'modUtilities.cmd_Clear_Consolidation



Sheets("Consolidation").Select


With Sheets("Consolidation")
.Unprotect ("estimate")
e10 = .Range("E10").Formula
e11 = .Range("E11").Formula
e12 = .Range("E12").Formula
e13 = .Range("E13").Formula
e17 = .Range("E17").Formula


.Range("E12") = LoanAmt ' loan amount
.Range("E11") = LoanAmt
.Range("E13") = PayAmt ' payment amount
.Range("E17") = ActualTerm ' actual term
.Range("E10") = InterestRate ' interest rate
totalinterest = .Range("E15")


.Range("E10").Formula = e10
.Range("E11").Formula = e11
.Range("E12").Formula = e12
.Range("E13").Formula = e13
.Range("E17").Formula = e17
.Protect ("estimate")

End With




GetTotalInterest = totalinterest

modUtilities.cmd_Clear_Consolidation
Sheets("Variable").Select
Application.ScreenUpdating = True


End Function

THANKS!

S