Renns.
When you delete a sheet, vbProject must be recompiled. This will happen when the procedure UpdateTheList is completed. You need to run a new procedure (CopySheet) by OnTime to complete the copy sheet with the old name.
Attention! Recompile destroys all variables! If you are using global variables, you have to rebuild them before or after copying sheet.
Sub UpdateTheList()
Dim LastUpdated As Date 'The last time the user's file was updated.
Dim MasterDate As Date 'The last time the master file was modified.
Dim MasterFilePath As String 'The location of the master file.
Dim MasterFileName As String 'The filename of the master file.
Dim UserBook As Workbook 'The user's copy of the file.
Dim MasterBook As Workbook 'The master copy of the file.
Dim SheetIndex As Integer 'The index number of the worksheet being replaced.
Dim strProcedure As String
MasterFilePath = ActiveWorkbook.Path & "\"
MasterFileName = "MasterList.xlsm"
If Not fileFolderExists(MasterFilePath & MasterFileName) Then
MsgBox "Sorry, the file " & MasterFileName & " was not found in this location:" & Chr(13) & Chr(13) & _
MasterFilePath & Chr(13) & Chr(13) & "Please place both UserList.xlsm and MasterList.xlsm in the same location and try again." & Chr(13) & "Stopping macro.", vbCritical, "Master File Not Found"
Exit Sub
End If
'### For the purposes of this example, set LastUpdated prior to MasterDate.
'LastUpdated = Format(wsTheList.Range("E6").Value, "yyyy-mm-dd") '<--- This won't even work if the CodeName has been altered.
LastUpdated = "2012-08-01"
MasterDate = Format(FileDateTime(MasterFilePath & MasterFileName), "yyyy-mm-dd")
'Compare the dates.
If MasterDate > LastUpdated Then 'If the worksheet needs to be updated...
Worksheets("The List").Activate
MsgBox "The master list was updated on " & Format(MasterDate, "mmmm d, yyyy") & "." & Chr(13) & "Click OK to update your list.", vbInformation, "List Update"
Set UserBook = ActiveWorkbook 'Identify the user's file.
If Not WorksheetCodeNameExists(UserBook, "wsTheList") Then
MsgBox "The worksheet's CodeName is no longer 'wsTheList', therfore your list can not be updated and the entire project no longer works." & Chr(13) & Chr(13) & _
"Click OK to change the CodeName from 'wsTheList1' back to 'wsTheList'." & Chr(13) & Chr(13) & _
"NOTE: CHANGING A WORKSHEET'S CODENAME DURING RUNTIME WILL NOT WORK ONCE THE VBPROJECT IS PROTECTED.", vbCritical, "Program Corrupted"
If WorksheetCodeNameExists(UserBook, "wsTheList1") Then
Call ChangeCodeName("wsTheList1", "wsTheList")
MsgBox "The CodeName has been changed back to 'wsTheList'." & Chr(13) & "You may now try again." & Chr(13) & Chr(13) & "Stopping macro.", vbCritical, "Reset"
Else
MsgBox "Apparently the worksheet's CodeName is neither 'wsTheList' or 'wsTheList1'. You'll have to figure it out what happened on your own." & Chr(13) & Chr(13) & _
"Stopping macro.", vbCritical, "Fubar"
End If
Else
wsTheList.Activate 'Go to the worksheet that is being replaced.
SheetIndex = wsTheList.Index 'Mark the location of the existing wsTheList worksheet.
Application.ScreenUpdating = False
Workbooks.Open (MasterFilePath & MasterFileName) 'Open the master file.
Set MasterBook = ActiveWorkbook 'Identify the master file.
'Delete the old worksheet in the user's file.
UserBook.Activate 'Go back to the user's file.
Application.EnableEvents = False
Application.DisplayAlerts = False
'### Method 1: Delete the worksheet:
wsTheList.Delete
''### Method 2: Rename the CodeName, then Delete the worksheet. (This works, but only if the VBProject is NOT protected):
' Call ChangeCodeName("wsTheList", "DeleteThis") 'Change the CodeName of the old worksheet to allow the replacement worksheet to have the identical CodeName (IMPORTANT).
' UserBook.Worksheets(Code2Name(UserBook, "DeleteThis")).Delete 'Delete the worksheet containing the old verses.
strProcedure = "'CopySheet """ & UserBook.Name & """,""" & _
MasterBook.Name & """," & SheetIndex & "'"
Application.OnTime Now, strProcedure
End If
End If
End Sub
Sub CopySheet(strUsrBk As String, strMstrBk As String, iShId As Integer)
Dim UserBook As Workbook 'The user's copy of the file.
Dim MasterBook As Workbook 'The master copy of the file.
Dim SheetIndex As Integer 'The index number of the worksheet being replaced.
Set UserBook = Workbooks(strUsrBk)
Set MasterBook = Workbooks(strMstrBk)
SheetIndex = iShId
Application.DisplayAlerts = True
Application.EnableEvents = True
'Copy the worksheet from the master file to the user's file.
MasterBook.Worksheets(Code2Name(MasterBook, "wsTheList")).Copy _
Before:=UserBook.Sheets(SheetIndex)
MasterBook.Close False 'Close the Master file, do not save changes.
ActiveSheet.Unprotect
ActiveSheet.Range("E6") = Format(Now(), "mmmm d, yyyy") 'Update the "Last Updated" date.
ActiveSheet.Protect
'Cleanup.
Set UserBook = Nothing 'Uninitialize
Set MasterBook = Nothing 'Uninitialize
Application.ScreenUpdating = True
MsgBox "You have successfully updated your list.", vbInformation, "Success!"
End Sub
I apologize for my language. I use a translator.
Artik
Bookmarks