In this VBA the interior color is not being changed: Option Explicit Private Sub…
- User
In this VBA the interior color is not being changed: Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wb As Workbook Dim ws As Worksheet
If ThisWorkbook.Name = "Service Providers Analysis" Then Set wb = Workbooks("Service Providers")
codeIf wb Is Nothing Then ' Service Provider workbook not open Exit Sub End If If Not wb Is Nothing Then ' Service Provider workbook open Set ws = wb.Sheets("Start Page") ws.Unprotect password:="edit" ws.Range("$P$23").Interior.Color = RGB(255, 255, 255) ' White ws.Protect password:="edit" End IfEnd If
End Sub
- Assistant
There is an issue with the code where the interior color of the cell is not being changed because the code is trying to change the interior color of a cell that is protected.
To resolve this issue, you need to unprotect the sheet before changing the interior color of the cell and then protect it again after the change. Here is the corrected code:
vbaOption Explicit Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim wb As Workbook Dim ws As Worksheet If ThisWorkbook.Name = "Service Providers Analysis" Then Set wb = ThisWorkbook ' Use ThisWorkbook instead of Workbooks("Service Providers") to refer to the workbook containing the VBA code If wb Is Nothing Then ' Service Provider workbook not open Exit Sub Else Set ws = wb.Sheets("Start Page") ws.Unprotect password:="edit" ws.Range("$P$23").Interior.Color = RGB(255, 255, 255) ' White ws.Protect password:="edit" End If End If End Sub - User
If Set wb = ThisWorkbook which is 'Service Providers Analysis', how will the code know how to make changes to the other workbook 'Service Providers'
- Assistant
Apologies for the oversight. If you need to make changes to the "Service Providers" workbook, you should set
wbto the "Service Providers" workbook instead ofThisWorkbook. You can open the "Service Providers" workbook within the code like this:vbaOption Explicit Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim wb As Workbook Dim ws As Worksheet If ThisWorkbook.Name = "Service Providers Analysis" Then On Error Resume Next Set wb = Workbooks("Service Providers") ' Try to set the "Service Providers" workbook On Error GoTo 0 If wb Is Nothing Then ' Service Providers workbook not open MsgBox "Service Providers workbook is not open." Else Set ws = wb.Sheets("Start Page") ws.Unprotect password:="edit" ws.Range("$P$23").Interior.Color = RGB(255, 255, 255) ' White ws.Protect password:="edit" End If End If End Sub - User
not changing the interior color of P23
- Assistant
It seems there might be an issue with changing the interior color of cell P23. One common reason for this issue is that the cell might not be within the visible range when the workbook is being closed.
Here is an alternative approach that ensures the "Service Providers" workbook is activated before making changes to the interior color of cell P23:
vbaOption Explicit Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim wb As Workbook Dim ws As Worksheet If ThisWorkbook.Name = "Service Providers Analysis" Then On Error Resume Next Set wb = Workbooks("Service Providers") On Error GoTo 0 If wb Is Nothing Then MsgBox "Service Providers workbook is not open." Else wb.Activate Set ws = wb.Sheets("Start Page") ws.Unprotect password:="edit" ws.Range("P23").Interior.Color = RGB(255, 255, 255) ' White ws.Protect password:="edit" End If End If End Sub