Results 1 to 4 of 4

Thread: Target.Value help in Worksheet_Change

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Target.Value help in Worksheet_Change

    Hello,

    I'm having an issue with some code in my Worksheet_Change event. Whenever I select several cells and fill down or right, or whenever I delete the contents of merged cells (which the macro unmerges automatically), I get a debug error because of a type mismatch. I would assume Target.Value is a variant (as it could be a string or number) and therefore can accept any data type, but I'm guessing it's not one because I keep getting the errors.

    Here is an example of my code. If you guys could tell me how to stop getting the type mismatch error, I would be very thankful.

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        Select Case Target.value
            Case "R", "r"
                Target.Interior.Color = RGB(255, 0, 0)  'background is red
                Target.Font.Color = RGB(0, 255, 255)    'font color is cyan
            Case "G", "g"
                Target.Interior.Color = RGB(0, 255, 0)  'background is green
                Target.Font.Color = RGB(255, 0, 255)    'font color is magenta
            Case "B", "b"
                Target.Interior.Color = RGB(0, 0, 255)  'background is blue
                Target.Font.Color = RGB(255, 255, 0)    'font color is yellow
                Range(Target.Address, Cells(Target.row + 1, Target.Column + 1)).Merge
            Case "" '
                Target.Interior.ColorIndex = 0
                Target.Font.Color = RGB(0, 0, 0)
                Range(Target.Address).UnMerge
        End Select
    End Sub

  2. #2
    Member
    Join Date
    Jun 2013
    Posts
    93
    Rep Power
    12
    what's the goal of

    Code:
            Case "" '
                Target.Interior.ColorIndex = 0
                Target.Font.Color = RGB(0, 0, 0)
                Range(Target.Address).UnMerge

  3. #3
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    add this line above the Select case statement

    Code:
    If Target.Cells.Count > 1 Then Exit Sub
    this will allow to exit the sub while working on multiple cells.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0
    Quote Originally Posted by patel View Post
    what's the goal of

    Code:
            Case "" '
                Target.Interior.ColorIndex = 0
                Target.Font.Color = RGB(0, 0, 0)
                Range(Target.Address).UnMerge
    Well, Case "" represents the deletion of cell contents, so it's supposed to set the background to no color and the font color to black, and then it's supposed to unmerge the cells. The background and font colors are changing only on the already unmerged cells, though.

    Quote Originally Posted by Admin View Post
    Hi

    add this line above the Select case statement

    Code:
    If Target.Cells.Count > 1 Then Exit Sub
    this will allow to exit the sub while working on multiple cells.
    Thank you, that worked.

Similar Threads

  1. Conditional Insert Query Based on Data in Target Table
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 06-23-2013, 04:24 PM
  2. Worksheet_Change event
    By Excelfun in forum Excel Help
    Replies: 2
    Last Post: 11-21-2012, 07:24 AM
  3. Event target range
    By bobkap in forum Excel Help
    Replies: 3
    Last Post: 09-13-2012, 05:34 AM
  4. Excel to Excel Data transfer without opening any of the files(source or target)
    By Transformer in forum Excel and VBA Tips and Tricks
    Replies: 14
    Last Post: 08-22-2012, 10:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •