+ Reply to Thread
Results 1 to 2 of 2

Code works when stepping through, but cuts out when run

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2005
    Posts
    2

    Code works when stepping through, but cuts out when run

    I have a sheet with names of files, and next to it numbers for passwords, like this:

    Anneke-Coetzer.xls 5913 5340

    Now if I step through this with F8 while in debug mode this code works. But when I run it (using my shortcut key), it doesn't... I believe it's because the sheet that opens gains focus, and it breaks the current macro.

    Sub PassWordSaveAll()
        drive = "c:\blah\test\temp\"
    
        'Note you have to select three cells for this. FileName, Password, Protect Password
        If Selection.Rows.Count = 1 And Selection.Columns.Count = 3 Then
            FileToSave = Selection.Cells(1, 1).Value
            BookPass = Selection.Cells(1, 2).Value
            SheetPass = Selection.Cells(1, 3).Value
                    
               'vvvv--- New sheet is opened here, and gains focus, and the rest is not executed
            If TryOpen(drive & FileToSave) Then
                  
                ' NewFile is opened
                Call ProtectAllSheetsAuto(SheetPass)
                
                ' Save The file with Password
                T = Application.DisplayAlerts
                'Application.DisplayAlerts = False
                ActiveWorkbook.SaveAs Filename:=drive & FileToSave, _
                FileFormat:=xlNormal, Password:=BookPass, WriteResPassword:="", _
                ReadOnlyRecommended:=False, CreateBackup:=False
                Application.DisplayAlerts = T
                ActiveWorkbook.Close
            End If
        End If
    End Sub
    Can anybody help me as to how I can get it to work when I press my shortcut key for this macro?

  2. #2
    Registered User
    Join Date
    04-21-2005
    Posts
    2
    I changed the code to this:

    If TryOpen("K:\Stats\2005\test\CO\" & FileToSave) Then

    ' NewFile is opened
    MsgBox (ActiveBook.Name)
    ProtectAllSheetsAuto (SheetPass)
    ... other code...
    Else
    MsgBox ("Failed to open " & FileToSave)
    End If

    and when I run it normally NEITHER of the MsgBoxes are shown.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1