0

I am using Macro Enabled file (binary sheet) which has many modules/forms and sometimes when something go wrong with my laptop and excel shuts down suddenly, my autosave files doesn't work.

I get that error on each autosaved file:

Run-time error '9'

Subscript out of range

My autosave frequency is 5 minutes to save my back but that interestingly doesn't work for this file.

I am even not able to follow where the error is because the only thing is opening on that autosaved file is White blank page. (That's why other Run-time error 9 questions on SO were not answer of my question) What kind of thing would be the reason and what is the possible solution?

UPDATE: Workbook_Open Events that I have in that Workbook

Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    ActiveWindow.Visible = False
    SplashUserForm.Show
    Windows(ThisWorkbook.Name).Visible = True
    Application.ScreenUpdating = True
    

   With Sheet5
        .Unprotect Password:=""
        .Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
        :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
        AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
        AllowUsingPivotTables:=True, UserInterfaceOnly:=True
        .EnableOutlining = True
    End With
    With Sheet16
        .Unprotect Password:=""
        .Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
        :=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
        AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
        AllowUsingPivotTables:=True, UserInterfaceOnly:=True
        .EnableOutlining = True
    End With
 
    
End Sub

And here what my SplashUserForm has:

Private Sub UserForm_Activate()
    Application.Wait (Now + TimeValue("00:00:01"))
    SplashUserForm.Label1.Caption = "Loading Data..."
    SplashUserForm.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    SplashUserForm.Label1.Caption = "Creating Forms..."
    SplashUserForm.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    SplashUserForm.Label1.Caption = "Opening..."
    SplashUserForm.Repaint
    Application.Wait (Now + TimeValue("00:00:01"))
    Unload SplashUserForm
End Sub


Private Sub UserForm_Initialize()

HideTitleBar Me
With Me
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
End With

End Sub

Option Explicit
Option Private Module

Public Const GWL_STYLE = -16
Public Const WS_CAPTION = &HC00000
Public Declare Function GetWindowLong _
                       Lib "user32" Alias "GetWindowLongA" ( _
                       ByVal hWnd As Long, _
                       ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong _
                       Lib "user32" Alias "SetWindowLongA" ( _
                       ByVal hWnd As Long, _
                       ByVal nIndex As Long, _
                       ByVal dwNewLong As Long) As Long
Public Declare Function DrawMenuBar _
                       Lib "user32" ( _
                       ByVal hWnd As Long) As Long
Public Declare Function FindWindowA _
                       Lib "user32" (ByVal lpClassName As String, _
                       ByVal lpWindowName As String) As Long

Sub HideTitleBar(frm As Object)
    Dim lngWindow As Long
    Dim lFrmHdl As Long
    lFrmHdl = FindWindowA(vbNullString, frm.Caption)
    lngWindow = GetWindowLong(lFrmHdl, GWL_STYLE)
    lngWindow = lngWindow And (Not WS_CAPTION)
    Call SetWindowLong(lFrmHdl, GWL_STYLE, lngWindow)
    Call DrawMenuBar(lFrmHdl)
End Sub
Community
  • 1
  • 1
Mertinc
  • 793
  • 2
  • 13
  • 27
  • Can you show your vba auto save code? – 0m3r Mar 01 '17 at 05:21
  • May be you should start adding text file log writing first thing in each Sub/Function to create tracks about where it went wrong. Painful but that's I locate whereabouts an error that causes Excel to crash itself. Then add more log or put breakpoints in that crashing Sub/Function. – PatricK Mar 01 '17 at 06:02
  • 0m3r, I don't have any auto save code on my file, I am talking about Excel's own Auto Recover system. That one works pretty well with all of other files (when anything happens) but doesn't work for this one. PatricK, thank you for your explanation but honestly I don't know how to create those tracks to follow where it goes wrong. And the thing is I have many modules and forms that not sure where to locate those breakpoints. And I need to wait for next excel crash (which cause AutoRecover) to track this thing and we don't know when it'll happen again. – Mertinc Mar 01 '17 at 06:40
  • Please show your BeforeSave or AfterSave functions, perhaps Excels autosave feature has a problem with one of them. – Julian Kuchlbauer Mar 01 '17 at 07:22
  • If you are getting that error when you **open** the file, do you have any Workbook_Open events? – YowE3K Mar 01 '17 at 07:38
  • @YowE3K That's correct, I also have Workbook_Open events. One of them is for Opening a Splash User Form on first opening, the other is to protect two sheets, if it's unprotected. There may be any thing because of them? – Mertinc Mar 03 '17 at 00:30
  • @JulianKuchlbauer how and where can I show BeforeSave or AfterSave functions? – Mertinc Mar 03 '17 at 00:31
  • @PatricK , thank you for your explanation but honestly I don't know how to create those tracks to follow where it goes wrong. And the thing is I have many modules and forms that not sure where to locate those breakpoints. And I need to wait for next excel crash (which cause AutoRecover) to track this thing and we don't know when it'll happen again. – Mertinc Mar 03 '17 at 00:31
  • If the error is happening when you **open** the files again, and you have `Workbook_Open` events, please edit the question and paste the code from those events into it so we can see whether anything there could be causing a subscript error. (It's possibly something trying to be accessed prior to the load completely finishing - I have heard that is often an issue with Workbook_Open, and the opening from an autorecovered version may be slowing things down and exacerbating the problem.) – YowE3K Mar 03 '17 at 02:21
  • @YowE3K I've just edited the question. As Workbook_Open event runs a user form and some macros related to that form, I also wrote the other codes as well. – Mertinc Mar 03 '17 at 04:37
  • I'm not an expert with Forms, but it seems strange to me that you are trying to hide the title bar while still initializing the form, which I assume is occurring as a precursor to it being displayed. I would have thought that `FindWindowA(vbNullString, frm.Caption)` wouldn't be able to find the window until it was actually created. – YowE3K Mar 03 '17 at 06:34
  • @YowE3K After 2 months hi again! I was still facing the same issue and today I remembered your suggestion and tried to delete every single parts of this splash screen process belong to Workbook_Open event. Finally I've come up with the reason: `Windows(ThisWorkbook.Name).Visible = True` I don't know how it became False but, when I try to open auto-recover file, I realize that file is opening normally, my form is opening normally, but when form disappears, the error comes through. The reason is excel somehow can't turn `Windows(ThisWorkbook.Name).Visible` to True. What do you think? – Mertinc May 02 '17 at 07:11
  • @YowE3K, I think it's better for you to write your suggestion (The problem would be on Workbook_Open event) as an answer, I'll accept it. Then as it's better to ask a new question for this Windows`(ThisWorkbook.Name).Visible = True` question, please refer to the new link [http://stackoverflow.com/questions/43732367/windowsthisworkbook-name-visible-true-causes-error-on-excels-auto-recovered] – Mertinc May 02 '17 at 07:28
  • @Mertinc - I would rather not post my guess as an "answer" as it really is a guess, and not having my "answer" there should make it (slightly) more likely that someone might notice the question and provide a real answer. P.S. Isn't your `Windows(ThisWorkbook.Name).Visible` being set to `False` by your `ActiveWindow.Visible = False` statement? (Again, I stress that I'm no expert in these type of matters, so I could easily be wrong.) – YowE3K May 02 '17 at 08:03
  • Finally the issue is solved. You can check the answer, thanks for your effort on trying to help me. – Mertinc May 03 '17 at 01:36

1 Answers1

0

After discussing with many people from many blogs, finally I came up with two solution. (First one is like error handling and the second one exactly solves the issue.) Special thanks to YowE3k,jkpieterse and Ryan Wells.

First of everything, I would like to mention the reason, why it was happening:

The code fails because when Excel recovers a file, it adds some text to the caption of the window, so that "FileName.xlsx" becomes something like "FileName.xlsx [Version last saved by user]" (jkpieterse).

Solutions:

1) Basic Error Handling (Ryan Wells)

If we know which line is causing error, we can comment out that line to protect our workbook. So if you comment out (or simply delete) the :

ActiveWindow.Visible = False

Windows(ThisWorkbook.Name).Visible = True

lines, that will stop the problem.

2) Original Solution (jkpieterse)

use a below routine in your ThisWorkbook object sheet.

Sub ShowaWindow(sFileName As String)
    Dim oWb as Workbook
    For Each oWb In Workbooks
        If lCase(owb.Name) = lCase(sFileName) Then
            oWb.Windows(1).Visible = True
            Exit For
        End If
    Next
End Sub

Then, in the Workbook_Open event,

Instead of Windows(ThisWorkbook.Name).Visible = True

UseShowaWindow(ThisWorkbook.Name)

Then it will work like a charm!

Mertinc
  • 793
  • 2
  • 13
  • 27