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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,104
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Haven't tested the code, BUT you don't have and End Select OR and End With Statement in your code
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
2,149
Office Version
  1. 2010
Platform
  1. Windows
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
 

Forum statistics

Threads
1,147,962
Messages
5,744,053
Members
423,843
Latest member
alex2022

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
Top