Worksheet_Change - Argument not optional

UnMrit

New Member
Joined
Aug 23, 2021
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi, I have been trying to update the color of corresponding tabs when the table column containing the status is changed. I have written the following code to achieve the same:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

  On Error Resume Next

  If Not Intersect(Target, Range("D:D")) Is Nothing Or Target.Cells.Count > 1 Then
  
    Dim mystat As String
    Dim mycell As String
    
    Application.EnableEvents = False
    
    mystat = Target.Value
    Target.Select
    mycell = "INC" & Range("B" & ActiveCell.Row).Value
    
    If WorksheetExists(mycell) Then
      
        If mystat = "In Progress" Then
          Worksheets(mycell).Tab.Color = vbYellow
        End If
        With Worksheets(mycell).Tab
          Select Case mystat
            Case "In Progress"
              .Color = RGB()
      
    End If
  End If
  
  Application.EnableEvents = True
  On Error GoTo 0
  
End Sub

Before the validations can be performed, however, I am getting an error that says: Compile Error - Argument not optional - the code stops at the method declaration line whenever I change the status of a cell in the D column (which contains the STATUS). Please try to point out what I am doing incorrectly here, thanks.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Haven't tested the code, BUT you don't have and End Select OR and End With Statement in your code
 
Upvote 0
this should compile if you have the worksheetexists function:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

  On Error Resume Next

  If Not Intersect(Target, Range("D:D")) Is Nothing Or Target.Cells.Count > 1 Then
 
    Dim mystat As String
    Dim mycell As String
   
    Application.EnableEvents = False
   
    mystat = Target.Value
    Target.Select
    mycell = "INC" & Range("B" & ActiveCell.Row).Value
   
    If WorksheetExists(mycell) Then
     
        If mystat = "In Progress" Then
          Worksheets(mycell).Tab.Color = vbYellow
        End If
        With Worksheets(mycell).Tab
          Select Case mystat
            Case "In Progress"
              .Color = RGB(255, 0, 0)  ' changed
            End Select   ' added
         End With        ' added
    End If
  End If
 
  Application.EnableEvents = True
  On Error GoTo 0
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top