2

I am having this really strange error in excel VBA that keeps showing up. I used the codes below

Public psword, oldpsword As Integer


Public Sub Auto_Open()

psword = Sheets("Main Menu").Cells(55, 1)
oldpsword = psword
For Each ws In Worksheets
    ws.Protect Password:=psword
Next ws

End Sub

Whenever the code goes to the line "oldpsword = psword" an error message pops up showing Run-time '13:' type mismatch error. How can i fix it?

2
  • 3
    In line Public psword, oldpsword As Integer only oldpsword is Integer while psword is Variant. Redim it Public psword As integer, oldpsword As Integer Commented May 2, 2014 at 12:08
  • I agree with simoco that you need to fix your Dim statement. However, I suspect the problem is that Cells(55,1) does not contain an integer. A variant can contain anything so psWord = ... is OK but oldpsword = psword fails because the value in psword cannot be converted to a integer. Commented May 4, 2014 at 19:42

2 Answers 2

2

Try replacing:

Public psword, oldpsword As Integer

with

Dim psword as Long, oldpsword as Long
Sign up to request clarification or add additional context in comments.

Comments

0

Just a general comment here as simoco and Gary's Student have got you laced up... If your passwords might be alphanumeric (a little foresight here might save you debug hours in the future, especially if your client is getting the passwords wrong haha), then you may want to use a combination of variant and the CStr (http://www.techonthenet.com/excel/formulas/cstr.php):

Public psword As Variant, oldpsword As Variant

Public Sub Auto_Open()

psword = ThisWorkbook.Sheets("Main Menu").Cells(55, 1)
oldpsword = psword
For Each ws In Worksheets
    ws.Protect Password:=CStr(psword)
Next ws

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.