Hello..
I don't know what happened. I was struggling with some code that kept failing, and eventually it crashed the program. At first - unable to restart it. Eventually managed to find a way, but only to grab changes and put them into an older copy. The recovery files had removed a couple ActiveX Command buttons, changing them into pictures. I manually caught up to where I was before-ish... did it a bit different and it was working even better now... and then the same thing. This time the file opens in Safe Mode for only a couple seconds and then crashes again. I can't keep it open long enough to make changes. The second time I had another copy, but still a couple hours of work lost.
Anyway, what I was doing both times was similar. Maybe someone will know an answer as I describe this....
My code...
Sub CheckUser()
Dim UserRow, SheetCol, UserCol As Long
Dim SheetNm As String
Dim ColCt As Long
Dim CkAdmin As Integer
UserCol = Range("UserName").Column
Dim Mycell As Range
CkAdmin = 0
With Sheet17
Set Mycell = Cells(.Range("AdmUsrCk").Value, UserCol) '<----------------This was what crashed my first worksheet.
If .Range("B8").Value = Empty Then 'Incorrect Username 'AdmUsrCk
MsgBox "Please enter a correct user name"
Exit Sub
End If
If ChangePwd = True Then '2nd Time thru, so new password saved in table
Mycell.Offset(0, 1).Value = MyNewPwd '<----------------This was what crashed my second worksheet.
ChangePwd = False
With frmLogin
.PswdTxt.Value = ""
.PswdLbl.Caption = "Password"
.HiddenLbl.Visible = False
.HiddenTxt.Visible = False
.PswdTxt.Value = ""
.HiddenTxt.Value = ""
End With
End If
If .Range("B7").Value <> True Then 'Incorrect password 'AdmPwdCk
MsgBox "Please enter a correct password"
Exit Sub
End If
If NewPwdReq Then 'user has requested new password while logging in, so...
With frmLogin '1st time thru: check password, modify form for new pswd, wait for entry
.PswdTxt.Value = ""
.PswdLbl.Caption = "New Password"
.HiddenLbl.Visible = True
.HiddenTxt.Visible = True
ChangePwd = True 'This sets variable for 2nd time thru.
End With
Exit Sub
End If
'Log-in is Complete - Now Set Sheets in either Editable, ReadOnly, or VeryHidden
frmLogin.Hide
UserRow = .Range("B8").Value 'User Row AdmUsrCk
'.Range("AdmUsrN,AdmPswd").ClearContents
CkAdmin = Application.WorksheetFunction.CountIf(Range("SecurityAdmin"), "Ð")
If CkAdmin = 0 Then .Range("AdminAdmin") = "Ð"
ColCt = .Cells(4, Columns.Count).End(xlToLeft).Column
Application.ScreenUpdating = False
For SheetCol = Range("SecurityAdmin").Column To ColCt
SheetNm = .Cells(4, SheetCol).Value 'Sheet Name
If SheetNm = "" Then Exit Sub
If .Cells(UserRow, SheetCol).Value = "Ð" Then
Sheets(SheetNm).Unprotect "123"
Sheets(SheetNm).Visible = xlSheetVisible
End If
If .Cells(UserRow, SheetCol).Value = "Ï" Then
Sheets(SheetNm).Protect "123"
Sheets(SheetNm).Visible = xlSheetVisible
End If
If .Cells(UserRow, SheetCol).Value = "x" Then Sheets(SheetNm).Visible = xlVeryHidden
Next SheetCol
Application.ScreenUpdating = True
End With
End Sub
First error..
For some strange reason the With Sheet17 command didn't help Cells(.Range) to know what sheet to go to. I had a solution, but it only worked until I made other needed changes (can't recall what they were), but alas, I was working on more solutions when it crashed.
So to fix error, before first crash, I finally added
Dim wks as worksheet
Set wks = Sheets("Security")
'and then further down edited the tough code as
Set Mycell = Cells(wks.Range("AdmUsrCk").Value, UserCol)
'Security' is the name of Sheet17. I believe that change worked. But only until I needed the Mycell - where I began the second crash.
Second error..
Mycell is the location of the UserName. The next column is the location of the Password. The code was supposed to put the value into "Security" on that cell. But instead it put the code into Sheets("Business"), the sheet that I started on. A VBA error code never came up. The code worked... except it saved to the wrong sheet. So I added a Dim for wk, and set that sheet equal to the Business Sheet. I may have used With Sheets("Security") as well. I was playing with the Immediate pane, and using that to try to figure out the issue... when it crashed.
I realize that Option Explicit may have helped. I have it on most modules, but since I borrowed 50% of the code on this sheet (not the part that caused the crash), I didn't think to add it.
It may also be pertinent to know the relationship between the two sheets. The Business sheet is like a start sheet, and on there I added the command button to open a UserForm: "frmLogin". The code for the Login is sent to "WBSecurity" module (code above). This module is located under Modules, not under the worksheet objects. The "Security" sheet is physically located beside the "Business" sheet if that matters.
My worksheet was autosaving to my OneDrive. Occasionally it would pause, and the tiny circle would run, that shows something is computing. When that circle is short, then everything works well. Both crashes began with that circle... something computing... then it gets stuck... the sheets go dim, and then it disappears and won't reopen.
It may have something to do with the protecting of sheets. I had that part working pretty smoothly... well, that is the borrowed code... each column representing a different sheet, and the three symbols in the table telling the code which of the three states to make that sheet.
Anyway, that's about all I can give you right now. Any ideas?
Thanks
Gary