I have a VBA code in my workbooy 'Service Providers.xlsm' that backs up to an macro free…
- User
I have a VBA code in my workbooy 'Service Providers.xlsm' that backs up to an macro free workbook 'Service Providers History.xlsx'.
When I run the code in 'Service Providers.xlsm', it for some reason password protects the sheets in 'Service Providers History.xlsx'.
Is there a way to stop this from happening.
Public Sub ServiceProvidersHistory()
Application.EnableEvents = False Application.ScreenUpdating = Flse Dim sourceWB As Workbook Dim sourceWS As Worksheet Dim historyWB As Workbook Dim historyWS As Worksheet Dim lastRow As Long Dim pasteRow As Long ' Set source workbook and worksheet Set sourceWB = ActiveWorkbook Set sourceWS = sourceWB.ActiveSheet Dim backupNeeded As Boolean 'backupNeeded = sourceWB.ActiveSheet.Range("J31").Value = "" backupNeeded = sourceWB.ActiveSheet.Range("J21").Value = "" If backupNeeded Then Dim userResponse As VbMsgBoxResult userResponse = MsgBox("CURRENT NUMBER OF ENTRIES DO NOT NEED A BACK UP" & vbCrLf & "" & vbCrLf & _ "Do you still want to back up?", vbQuestion + vbYesNo) If userResponse = vbNo Then Exit Sub End If End If ' Insert today's date in C5 to I5 Dim currentDate As Date currentDate = Date ' Unlock Sheet sourceWS.Unprotect Password:="edit" sourceWS.Range("C5").Value = currentDate sourceWS.Range("I5").Value = currentDate ' Lock Sheet sourceWS.Protect Password:="edit" ' Find the first empty cell in column IDim firstEmptyCell As Range Set firstEmptyCell = sourceWS.Range("I:I").Find("", LookIn:=xlValues, LookAt:=xlWhole) Dim dateCell As Range Set dateCell = firstEmptyCell.Offset(-1, 0) If Format(dateCell.Value, "dd / mm / yyyy") < Date - 30 Then MsgBox "Completed Tasks are recent, wait 30 days before committing to History" Application.EnableEvents = True Exit Sub End If
' Check if an empty cell is found If Not firstEmptyCell Is Nothing Then ' Calculate the last row to copy based on the first empty cell in column I lastRow = firstEmptyCell.Row - 1 End If ' Copy the range from B5 to the last row in L sourceWS.Range("B5:L" & lastRow).Copy ' Open the history workbook ' USING RELATIVE PATHDim filePath As String filePath = ThisWorkbook.Path & "\Service Providers History.xlsx" Set historyWB = Workbooks.Open(filePath)
Application.Wait (Now + TimeValue("0:00:15")) ' Select the sheet with the same name as the active sheet in the source workbook On Error Resume Next Set historyWS = historyWB.Sheets(sourceWS.Name) On Error GoTo 0 ' If the sheet doesn't exist, create a new sheet with the same name If historyWS Is Nothing Then sourceWS.Copy After:=historyWB.Sheets(historyWB.Sheets.count) Set historyWS = historyWB.ActiveSheet historyWS.Name = sourceWS.Name 'CREATE A NEW SHEET IN HISTORY WITH FORMATING ' Copy values, formatting and column widths of the range B1:L5 'sourceWS.Range("B1:L5").Copy 'historyWS.Range("B1:L5").PasteSpecial xlPasteValuesAndNumberFormats 'historyWS.Range("B1:L5").PasteSpecial xlPasteColumnWidths - Assistant
The issue of password protecting sheets in 'Service Providers History.xlsx' can be caused by the sheet protection settings in the source workbook. To stop this from happening, you can modify the code to disable sheet protection before copying the range and re-enable it afterwards.
Add the following lines of code before copying the range:
' Unprotect the history workbook sheets historyWS.Unprotect Password:="password"And add the following lines of code after pasting the values into the history sheet:
' Protect the history workbook sheets historyWS.Protect Password:="password" - User
I do not want the sheets in the workbook 'Service Providers History.xlsx' to have any password protection.
- Assistant
Apologies for the confusion. To remove the password protection from sheets in 'Service Providers History.xlsx', you can modify the code accordingly.
You need to remove the lines that unprotect and protect the history workbook sheets. Here's the updated code:
Public Sub ServiceProvidersHistory() ' Rest of the code... ' Open the history workbook ' USING RELATIVE PATH Dim filePath As String filePath = ThisWorkbook.Path & "\Service Providers History.xlsx" Set historyWB = Workbooks.Open(filePath) ' Select the sheet with the same name as the active sheet in the source workbook On Error Resume Next Set historyWS = historyWB.Sheets(sourceWS.Name) On Error GoTo 0 ' If the sheet doesn't exist, create a new sheet with the same name If historyWS Is Nothing Then ' Rest of the code... End If ' Paste the values from the clipboard into the history sheet historyWS.Range("B" & pasteRow).PasteSpecial Paste:=xlPasteValues ' Rest of the code... End SubWith this modification, the sheets in 'Service Providers History.xlsx' will not have any password protection applied when running the code in 'Service Providers.xlsm'.