SensualCarrots
New Member
- Joined
- Mar 21, 2015
- Messages
- 46
I have two separate workbooks with different scenarios that I am seeing a consistent problem with. Both workbooks have code in the Workbook_Open subroutine.
The first one sets sheet2 to visible, then sheet 1 to very hidden, then selects a range on sheet2, forcing the user to enable macros to see the content. The logic is reversed when exiting and works fine.
The second one run Application.Calculate as the first line of the workbook open because of a formula that returns a #N/A error until the workbook is calculated.
In both scenarios, if I make a new copy of the workbook (or send it to someone else), when initially opening the workbook, and enabling the macros, I get errors. If I save (and have trusted documents turned on) and re-open as to not get the enable content banner, the code executes fine with no error. So I decided to step through the subroutine to see what is going on. Strange enough, when stepping through the first one after enabling the content, the sheets do not respond to the visible/very hidden command until the subroutine completes. This means that when trying to select a range (also tried activating sheet2 after making it visible, this also fails) on sheet2, it fails because sheet2 has not actually been made visible yet. So I saved, closed, reopened, and stepped through the subroutine again, and this time when passing through the worksheet.visible properties, both sheets responded immediately after the line was executed, and the range.select property passed with no problem. Is this something new? I'm using Office 365 64bit. The problem is that we pass documents back and forth quite frequently, and everytime I or someone else opens a new document that isn't trusted, it is riddled with errors. Note that when calling other subroutines from the workbook.open subroutine, the same behavior occurs, and the workbook doesn't respond to the code until the workbook.open subroutine completes.
I've created a file I made from a template, but not sure how to upload it. Below is my code. If you try to recreate this, you'll have to find a way to make the document untrusted (apparently, even if you have trusted documents turned off, but you are the author, you won't get the warning). I did this by uploading it to onedrive then downloading a copy from the site to my desktop. After that I get the warning.
Start by renaming Sheet1 (Code Name and Logical Name) to LockSheet. Then add a new sheet and make sure the Code Name and Logical Name are Sheet1.
The code below goes in the 'ThisWorkbook' object:
The below code goes in a Module I called 'PublicVariables'
Notice when you first open it, it fails on the Sheet1.Range("A1").Select If you stop the debugger, then save and reopen the file (assuming it becomes a trusted document), the error does not occur.
Any thoughts?
The first one sets sheet2 to visible, then sheet 1 to very hidden, then selects a range on sheet2, forcing the user to enable macros to see the content. The logic is reversed when exiting and works fine.
The second one run Application.Calculate as the first line of the workbook open because of a formula that returns a #N/A error until the workbook is calculated.
In both scenarios, if I make a new copy of the workbook (or send it to someone else), when initially opening the workbook, and enabling the macros, I get errors. If I save (and have trusted documents turned on) and re-open as to not get the enable content banner, the code executes fine with no error. So I decided to step through the subroutine to see what is going on. Strange enough, when stepping through the first one after enabling the content, the sheets do not respond to the visible/very hidden command until the subroutine completes. This means that when trying to select a range (also tried activating sheet2 after making it visible, this also fails) on sheet2, it fails because sheet2 has not actually been made visible yet. So I saved, closed, reopened, and stepped through the subroutine again, and this time when passing through the worksheet.visible properties, both sheets responded immediately after the line was executed, and the range.select property passed with no problem. Is this something new? I'm using Office 365 64bit. The problem is that we pass documents back and forth quite frequently, and everytime I or someone else opens a new document that isn't trusted, it is riddled with errors. Note that when calling other subroutines from the workbook.open subroutine, the same behavior occurs, and the workbook doesn't respond to the code until the workbook.open subroutine completes.
I've created a file I made from a template, but not sure how to upload it. Below is my code. If you try to recreate this, you'll have to find a way to make the document untrusted (apparently, even if you have trusted documents turned off, but you are the author, you won't get the warning). I did this by uploading it to onedrive then downloading a copy from the site to my desktop. After that I get the warning.
Start by renaming Sheet1 (Code Name and Logical Name) to LockSheet. Then add a new sheet and make sure the Code Name and Logical Name are Sheet1.
The code below goes in the 'ThisWorkbook' object:
VBA Code:
Private Sub Workbook_Open()
LockSheet.Unprotect
For Each ws In ThisWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
LockSheet.Visible = xlSheetVeryHidden
Sheet1.Range("A1").Select
wbChanged = False
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
wbChanged = True
End Sub
Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object)
wbChanged = True
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
wbChanged = True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If wbChanged = True And ThisWorkbook.Saved = False Then
answer = MsgBox("Want to save your changes to '" & ThisWorkbook.Name & "'?", vbYesNoCancel + vbExclamation, "Microsoft Excel")
If answer = vbYes Then
wbChanged = False
ElseIf answer = vbNo Then
noSave = True
Else
Cancel = True
Exit Sub
End If
Else
saveAtEnd = True
End If
LockSheet.Visible = xlSheetVisible
LockSheet.Protect
For Each ws In ThisWorkbook.Worksheets
If InStr(1, ws.CodeName, "LockSheet") = 0 Then
ws.Visible = xlSheetVeryHidden
End If
Next ws
If wbChanged = False Or saveAtEnd = True Then ThisWorkbook.Save
If noSave = True Then ThisWorkbook.Saved = True
End Sub
The below code goes in a Module I called 'PublicVariables'
VBA Code:
Option Explicit
Public wbChanged As Boolean
Public ws As Worksheet
Public answer As Variant
Public noSave
Public saveAtEnd
Notice when you first open it, it fails on the Sheet1.Range("A1").Select If you stop the debugger, then save and reopen the file (assuming it becomes a trusted document), the error does not occur.
Any thoughts?