+ Reply to Thread
Results 1 to 8 of 8

Two workbooks open, code changing wrong Wb, help!

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Two workbooks open, code changing wrong Wb, help!

    Hi, I have the following code and at line 200 the wrong workbook is having cells unprotected. How do I make the "final" workbook change and not the "Backup"? Having a brain fart today Thanks for the help!

    Private Sub Mill_Reports_Final()
    
           'declare varibales
              Dim Wb        As Workbook
              Dim Ws        As Worksheet
              Dim strFile   As String
              
              'turn off the screen-updating to _
               done everything in the backend
    10        Application.ScreenUpdating = False
    20        Application.DisplayAlerts = False
              
    30        Sheet6.Visible = xlSheetVeryHidden
    40        Sheet8.Visible = xlSheetVeryHidden
    50        Sheet4.Shapes.Range(Array("Button 1")).Visible = msoFalse
              'Selection.Delete
    
    60        Set Wb = Workbooks.Open(Environ("USERPROFILE") & "\Documents\Mill Four Sheets_Backup_" & Sheet2.Range("B4").Value & ".xlsm")
    70        strFile = Environ("USERPROFILE") & "\Documents\Mill Four Sheets_Final_" & Sheet2.Range("B4").Value & ".xlsm"
              'Delete previous _Final file if exists
    80        If Len(Dir(strFile)) Then Kill strFile
              'Save the workbook after protecting
    90        Wb.SaveCopyAs fileName:=strFile ', _
                        FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
                        'Password:="", WriteResPassword:="", _
                        'ReadOnlyRecommended:=False, CreateBackup:=False
              
              'Wb.Close SaveChanges:=False 'just close it. No need to save it again
              
    100               If Sheet1.Range("AG1").Value = "D" Then
    110                   Sheet8.Visible = xlSheetVisible
                          'Sheet4.Range("AA1").Value = ""
    120               ElseIf Sheet1.Range("AG1").Value = "N" Then
    130                   Sheet6.Visible = xlSheetVisible
                          'Sheet4.Range("AA1").Value = ""
    140               End If
    150       Sheet4.Shapes.Range(Array("Button 1")).Visible = msoTrue
              
              'open the workbook that you want to protect
    160       Set Wb = Workbooks.Open(Environ("USERPROFILE") & "\Documents\Mill Four Sheets_Final_" & Sheet2.Range("B4").Value & ".xlsm")
              
    170       For Each Ws In Wb.Worksheets
    180           Ws.Cells.Locked = True
                  'Ws.Protect "Pword"
    190       Next Ws
              
              
    200       With Sheet1
    210           .Unprotect "DjS"
    220           .Range("L9:M15").Locked = False
    230           .Range("N7:N17").Locked = False
    240           .Range("N25:N28").Locked = False
    250           .Protect "DjS"
    260       End With
              
    270       If ActiveWorkbook.Sheets("Sheet5").Visible = xlSheetVisible Then
    280           With Sheet5
    290           .Unprotect "DjS"
    300           .Range("L9:M15").Locked = False
    310           .Range("N7:N17").Locked = False
    320           .Range("N25:N28").Locked = False
    330           .Protect "DjS"
    340           End With
    350       End If
              
    360       Application.EnableEvents = False
              
              'Workbooks.Open (Environ("USERPROFILE") & "\Documents\Mill Four Sheets_Backup_" & Sheet2.Range("B4").Value & ".xlsm")
    370       Wb.Close SaveChanges:=True
              
              'turn on the screen-updating
    380       Application.ScreenUpdating = True
    390       Application.DisplayAlerts = True
    400       Application.EnableEvents = True
    
    End Sub
    Last edited by thecdnmole; 12-19-2020 at 03:19 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Two workbooks open, code changing wrong Wb, help!

    You need to add a workbook reference to the code to make sure it executes on the correct workbook.
    With Wb.Sheets("sheet1")
            .Unprotect "DjS"
            .Range("L9:M15").Locked = False
            .Range("N7:N17").Locked = False
            .Range("N25:N28").Locked = False
            .Protect "DjS"
        End With
    
        If Wb.Sheets("Sheet5").Visible = xlSheetVisible Then
            With Wb.Sheets("Sheet5")
                .Unprotect "DjS"
                .Range("L9:M15").Locked = False
                .Range("N7:N17").Locked = False
                .Range("N25:N28").Locked = False
                .Protect "DjS"
            End With
        End If
    
        Application.EnableEvents = False
    
        'Workbooks.Open (Environ("USERPROFILE") & "\Documents\Mill Four Sheets_Backup_" & Sheet2.Range("B4").Value & ".xlsm")
        Wb.Close SaveChanges:=True
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: Two workbooks open, code changing wrong Wb, help!

    Thanks Norie, I tried that and get "Runtime error 9""Subscript out of Range".

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,132

    Re: Two workbooks open, code changing wrong Wb, help!

    You are using the sheet codename, but you can only use that directly for the workbook containing the code.
    So you need to use the actual sheet name as shown by Norie, if you are getting an RTE 9 then it means the sheet name is wrong.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Two workbooks open, code changing wrong Wb, help!

    You need to use the sheet tab names rather than codenames.

    So change Sheets("Sheet1") to Sheets(tabname) where tabname is the tab name of the sheet with the code name 'Sheet1'.

  6. #6
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: Two workbooks open, code changing wrong Wb, help!

    Quote Originally Posted by Norie View Post
    You need to use the sheet tab names rather than codenames.

    So change Sheets("Sheet1") to Sheets(tabname) where tabname is the tab name of the sheet with the code name 'Sheet1'.
    Odd, I tried that as well with same error.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Two workbooks open, code changing wrong Wb, help!

    Where are you getting the error?

    Have you checked the sheet names for leading/trailing spaces?

  8. #8
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: Two workbooks open, code changing wrong Wb, help!

    Good grief, I entered the tab names and did not think I put a space, but yes, there was a space!! Thanks for your help Norie.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Date changing to wrong month in code
    By heathb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-05-2019, 04:25 AM
  2. [SOLVED] Workbooks.open number format wrong
    By Teddi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-18-2019, 08:15 AM
  3. [SOLVED] Using a Variable FileName in macro to open file but wrong code to close it
    By cljohnston64 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-19-2015, 01:15 PM
  4. [SOLVED] What is wrong with this code? (Loop through WBs in folder - open, edit, save & close)
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-04-2013, 07:49 PM
  5. [SOLVED] whats wrong with this code: need to open picture from folder in IE
    By Lkivagten in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2012, 05:32 PM
  6. My code try to open other workbooks.
    By Cabeza2000 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-14-2007, 08:09 AM
  7. [SOLVED] Excel passwords are changing when two workbooks are open.
    By Password in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 12-21-2005, 12:35 PM

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