Domanda

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?

È stato utile?

Soluzione

Try replacing:

Public psword, oldpsword As Integer

with

Dim psword as Long, oldpsword as Long

Altri suggerimenti

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top