Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:01:21+00:00 2026-06-14T17:01:21+00:00

I have a spreadsheet set up like this: 1 Basic Rota 09:00 13:00 2

  • 0

I have a spreadsheet set up like this:

1   Basic Rota  09:00   13:00
2   Absence           S 

If you imagine the column labels start above ‘Basic Rota’ as A, B and C. The Absence cell (B2:C2) is a merged cell which can contain either ‘H’,’S’,’T’,’SC’ or it can be empty. Based on the contents of that cell, B1 and C1 should change colour. I have a bit of VBA which does the job.

Option Compare Text 'A=a, B=b, ... Z=z
Option Explicit


Private Sub Worksheet_Change(ByVal Selection As Range)

        Select Case Target.Value

    Case "S"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 53
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 53
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 53
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 53

    Case "H"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 50
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 50
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 50
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 50

    Case "T"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 44
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 44
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 44
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 44

    Case "SC"

        Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 42
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 42
        Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 42
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 42

    Case Else

        Target.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
        Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone

End Select

End Sub

However, if the contents of the merged cell (B2:C2) are deleted, I receive an error (Run-time error ’13’: Type Mistmatch) on the line ‘Case “S”
‘. I can get around it with an ‘On Error GoTo’ line, but it means that the cell that has been conditionally formatted doesn’t get returned to ‘no fill’. This isn’t an issue if it’s done on cells that aren’t merged, so it could be that I need to stop using merged cells all together – however, for user friendliness it’d be nice to keep it (rather than making the user input ‘H’ twice in B2 and C2 for example). For reference, this is for Excel 2003. I should add that the macro is added to a worksheet by viewing the code for that worksheet and is based on worksheet_change.

If anyone could assist on this it’d be much appreciated!

Edit: Answer below based on @Philip A Barnes’ answer.

  Private Sub Worksheet_Change(ByVal Target As Range)


  Select Case Target.Columns(1).Value

  Case Empty

    Target.Columns(1).MergeArea.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
    Target.Columns(1).MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone

Case "S"

    Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 53
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 53
    Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 53
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 53

Case "H"

    Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 50
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 50
    Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 50
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 50

Case "T"

    Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 44
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 44
    Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 44
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 44

Case "SC"

    Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 42
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 42
    Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 42
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 42

Case Else

    Target.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
    Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone

End Select

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:01:22+00:00Added an answer on June 14, 2026 at 5:01 pm

    this is because when you have no data in the cell the Target reference returns “Empty”. You need to extend your case statement to check for this:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    
    Select Case Target.Columns(1).Value
    
        Case Empty
    
            Target.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone
    
        Case "S"
    
            Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 53
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 53
            Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 53
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 53
    
        Case "H"
    
            Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 50
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 50
            Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 50
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 50
    
        Case "T"
    
            Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 44
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 44
            Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 44
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 44
    
        Case "SC"
    
            Target.MergeArea.Offset(-1, 0).Interior.ColorIndex = 42
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = 42
            Target.MergeArea.Offset(-1, 0).Font.ColorIndex = 42
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Font.ColorIndex = 42
    
        Case Else
    
            Target.Offset(-1, 0).Interior.ColorIndex = xlNone 'No Fill
            Target.MergeArea.Offset(-1, 1).Offset(0, -1).Interior.ColorIndex = xlNone
    
    End Select
    
    End Sub
    

    Make sure it is the first check you do. Also I would suggest looking into Excels built in Conditional Formatting which you can manipulate using VBA.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My spreadsheet is set up like this: I have a list of job numbers
I have an Excel Spreadsheet like this id | data for id | more
We have an application set up like this: server - client where the client
I have data in an Excel spreadsheet with values like this: 0.69491375 0.31220394 The
Like many, I have spreadsheet that draws data from over 40 text files as
I have a spreadsheet that I am sorting based on an item number column.
I have a spreadsheet where I would like to model the progress of several
I have a spreadsheet I created mid-August which has a table with one column
I have an excel spreadsheet that has dates in the first column, the rest
I have this formula in a cell: =GetData(Channel_01,Chicago) Which executes this code: Public Function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.