Consulting

Results 1 to 7 of 7

Thread: Code crashes when closing workbook

  1. #1
    VBAX Contributor Glaswegian's Avatar
    Joined
    Sep 2004
    Location
    Glasgow, Scotland
    Posts
    196
    Location

    Code crashes when closing workbook

    Hi all

    I'm creating a workbook that will allow users to amend an existing workbook. Since I don't know how users will have named their files I'm using GetOpenFilename and letting them select the relevant file. This is the code
    Private Sub CommandButton1_Click()
    Dim myFile
    myFile = Application.GetOpenFilename("Excel Files (*.xls),*.xls")
        If myFile = False Then
            MsgBox "No files were selected - please try again.", vbCritical + vbOKOnly, "Warning"
                Exit Sub
        End If
    Workbooks.Open myFile
    Application.ScreenUpdating = False
    With ActiveWorkbook.Sheets("Sheet3")
        .Visible = True
        .Range("C23").Value = "E"
        .Range("C24").Value = "F"
        .Range("C25").Value = "G"
        .Range("C26").Value = "H"
        .Range("C27").Value = "I"
        .Range("C28").Value = "J"
        .Range("C18:C28").Name = "LendCat"
        .Visible = False
    End With
    ActiveWorkbook.Close True
    Application.ScreenUpdating = True
    End Sub
    The problem occurs when the above code is closing the amended workbook. The second workbook has code that writes to a Log file and it crashes after the Unprotect line. This is the code in the second workbook - it will be the same no matter to what users have renamed the file.
    With Sheets("Log")
        .Unprotect Password:="*****"
        .Range("F65536").End(xlUp).Offset(1, 0).Value = Date <---crashes on this line
        .Range("G65536").End(xlUp).Offset(1, 0).Value = Time
        .Range("H65536").End(xlUp).Offset(1, 0).Value = UserName
        .Range("I65536").End(xlUp).Offset(1, 0).Value = NameOfComputer
        .Protect Password:="*****"
    End With
    I can't see any particular reason for this as this second workbook will be opened and closed on a regular basis. Can anyone help?

    Thanks
    Iain - XL2010 on Windows 7

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You don't say what the error is so it's guesswork but maybe you should try actually saving the workbook before you try to close it instead of closing and saving changes which might be causing a conflict with the workbook close event code......just guessing.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Contributor Glaswegian's Avatar
    Joined
    Sep 2004
    Location
    Glasgow, Scotland
    Posts
    196
    Location
    Ooops! Sorry about that - it's the old 1004 error - "the cell you're trying to change is protected etc..."

    Thanks - I'll give your suggestion a go. When using the second workbook on it's own, you can save and close it no problem. That's what I don't understand.
    Iain - XL2010 on Windows 7

  4. #4
    VBAX Contributor Glaswegian's Avatar
    Joined
    Sep 2004
    Location
    Glasgow, Scotland
    Posts
    196
    Location
    No joy, I'm afraid - still the same 1004 error. Weird...
    Iain - XL2010 on Windows 7

  5. #5
    VBAX Regular
    Joined
    Jan 2006
    Posts
    21
    Location
    Not sure if it will help, but how about running the code in the second workbook from itself? If it works, at least you know the error comes from your first code. Then you can work on it.

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Ooops! Sorry about that - it's the old 1004 error - "the cell you're trying to change is protected etc..."
    actually it is crashing on the very first line instead of the line you point out........it's not unprotecting.

    You tried something like this:
    [VBA]
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Regular
    Joined
    Jan 2006
    Posts
    21
    Location
    In your first workbook, look at the following line of code

    [VBA]
    With ActiveWorkbook.Sheets("Sheet3")
    [/VBA]

    If Sheet3 is protected, then you have to add something like

    [VBA]
    With ActiveWorkbook.Sheets("Sheet3")
    .Unprotect Password:="*****"
    'rest of the code
    [/VBA]

    so that the 1004 error will go away.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •