I would like to add to an existing VBA code a condition as described below. If the…
- User
I would like to add to an existing VBA code a condition as described below.
If the workbook 'Schedules' is open, then unlock the sheet 'Notes' in the workbook 'Schedules' using the password 'edit'
Can you please suggest.
- Assistant
Certainly! You can use the following code to check if the workbook 'Schedules' is open and unlock the 'Notes' sheet with the password 'edit':
Sub UnlockSheet() Dim wb As Workbook Dim ws As Worksheet ' Check if 'Schedules' workbook is open On Error Resume Next Set wb = Workbooks("Schedules.xlsx") ' Update the workbook name as needed On Error GoTo 0 ' If 'Schedules' workbook is open, unlock the 'Notes' sheet If Not wb Is Nothing Then Set ws = wb.Sheets("Notes") ' Update the sheet name as needed ws.Unprotect Password:="edit" End If End SubMake sure to replace "Schedules.xlsx" with the actual name of the workbook you want to check, and "Notes" with the name of the sheet you want to unlock.
- User
can you also give me a single line of VBA code that does the following; Workbook 'Schedules' Sheet 'Notes' Unprotect Password:="edit"
- Assistant
Certainly! Here's a single line of VBA code that unprotects the 'Notes' sheet in the 'Schedules' workbook with the password 'edit':
Workbooks("Schedules.xlsx").Sheets("Notes").Unprotect Password:="edit"Make sure to replace "Schedules.xlsx" with the actual name of your workbook, and "Notes" with the name of your sheet.