Home » excel » excel – Getting Run-time error '13': Type mismatch when I run following macro

excel – Getting Run-time error '13': Type mismatch when I run following macro

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am new to VBA and I am not quite familiar with syntax. I have made an excel macro that will identify the background of current cell and append the current value with the name of background color. So if a cell has blue background and value 1000, macro should replace the value in cell as ‘1000 blue’. Following is the code I wrote.

Sub coloridentifier()
Dim Myrange As Range
Dim Mycell As Range
Dim Myvalue As String
Set Myrange = Selection
For Each Mycell In Myrange.Rows
    Myvalue = Mycell.Value
    If Mycell.Interior.Color = RGB(0, 176, 80) Then
        Mycell.Value = Myvalue & " Green"
    ElseIf Mycell.Interior.Color = RGB(184, 204, 228) Then
        Mycell.Value = Myvalue & " Blue"
    ElseIf Mycell.Interior.Color = RGB(192, 0, 0) Then
        Mycell.Value = Myvalue & " Red"
    End If
Next Mycell
End Sub

The debugger points to following line:

Myvalue = Mycell.Value

What am I doing wrong here?

How to&Answers:

Change For Each Mycell In Myrange.Rows this line to

For Each Mycell In Myrange

When you are using Myrange.Rows Then the sub will loop to row of your set range. When you will use only Myrange then it will loop to cells of set range. Also remove .value from Mycell.Value because when you will use .value then it will take only values (numeric values) from cell. If you want to keep .value then change variable type Integer or Long from String like Dim Myvalue As Integer.

So, full sub will be

Sub coloridentifier()
Dim Myrange As Range
Dim Mycell As Range
Dim Myvalue As Variant
Set Myrange = Selection
For Each Mycell In Myrange
    Myvalue = Mycell
    If Mycell.Interior.Color = RGB(0, 176, 80) Then
        Mycell.Value = Myvalue & " Green"
    ElseIf Mycell.Interior.Color = RGB(0, 0, 255) Then
        Mycell.Value = Myvalue & " Blue"
    ElseIf Mycell.Interior.Color = RGB(192, 0, 0) Then
        Mycell.Value = Myvalue & " Red"
    End If
Next Mycell
End Sub

Answer:

You don’t need to declare a variable for the cell value, you can just do it like this:

Option Explicit
Sub coloridentifier()

    Dim Myrange As Range
    Dim Mycell As Range

    Set Myrange = Selection
    For Each Mycell In Myrange
        If Mycell.Interior.Color = RGB(0, 176, 80) Then
            Mycell = Mycell & " Green"
        ElseIf Mycell.Interior.Color = RGB(184, 204, 228) Then
            Mycell = Mycell & " Blue"
        ElseIf Mycell.Interior.Color = RGB(192, 0, 0) Then
            Mycell = Mycell & " Red"
        End If
    Next Mycell

End Sub

Though I’d avoid using the Set Myrange = Selection and declare your range by giving it the adresses.

Answer:

You’re trying to assign an array to a string variable

Cells is a collection of Range objects, so are Rows and Columns. Whenever you loop through Range.Rows and you have more than 1 column in a row, the .Value property will be an array and you can’t assign one to a string variable.