Type Mismatch Error

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I've suddenly started getting an Error 13 - Type Mismatch on this code, which is odd because it's worked fine for weeks;

Code:
Private Sub Worksheet_Activate()
    Dim ThisCell2 As Range
    Range("K2").Formula = "=TODAY()"
    Range("K4:K44").ClearContents
    For Each cell In Range("J4:J44")
        If cell = "" Then
            cell.Offset(0, 1).Value = ""
        End If
        For Each ThisCell2 In Sheet2.Range("J2:J8")
            If InStr(cell.Value, ThisCell2.Value) <> 0 Then
                cell.Offset(0, 1).Value = "Available"
            End If
        Next ThisCell2
        For Each ThisCell2 In Sheet2.Range("K2:K21")
            If InStr(cell.Value, ThisCell2.Value) <> 0 Then
                cell.Offset(0, 1).Value = "Unavailable"
            End If
        Next ThisCell2
    Next
    For Each cell In Range("J4:J44")
        If cell.Offset(0, 1) = "" Then
            Set FoundRange = Sheet2.Range("J2:K22").Find(what:=cell, LookIn:=xlFormulas, lookat:=xlWhole)
            If FoundRange Is Nothing Then
                cell.Offset(0, 1) = "Unavailable"
            End If
        End If
    Next
End Sub

It's failing on the following lines;

Code:
If cell = "" Then

and

Code:
If InStr(cell.Value, ThisCell2.Value) <> 0 Then

Can anyone offer an explanation and ideally a solution?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Most likely cause would be an error in one of the cells you are processing. Test first using
Code:
If Not IsError(cell.value) then
 
Upvote 0
How and where did you try that?
 
Upvote 0
I added this;

Code:
If Not IsError(cell.value) then

before my original code then added

Code:
End If

afterwards...this is on the worksheet activate event.
 
Upvote 0
Where specifically, please?
 
Upvote 0
Like this;

Code:
Private Sub Worksheet_Activate()
    Dim ThisCell2 As Range
    Range("K2").Formula = "=TODAY()"
    Range("K4:K44").ClearContents
    If Not IsError(cell.Value) Then
        For Each cell In Range("J4:J44")
            If cell = "" Then
                cell.Offset(0, 1).Value = ""
            End If
            For Each ThisCell2 In Sheet2.Range("J2:J8")
                If InStr(cell.Value, ThisCell2.Value) <> 0 Then
                    cell.Offset(0, 1).Value = "Available"
                End If
            Next ThisCell2
            For Each ThisCell2 In Sheet2.Range("K2:K21")
                If InStr(cell.Value, ThisCell2.Value) <> 0 Then
                    cell.Offset(0, 1).Value = "Unavailable"
                End If
            Next ThisCell2
        Next
        For Each cell In Range("J4:J44")
            If cell.Offset(0, 1) = "" Then
                Set FoundRange = Sheet2.Range("J2:K22").Find(what:=cell, LookIn:=xlFormulas, lookat:=xlWhole)
                If FoundRange Is Nothing Then
                    cell.Offset(0, 1) = "Unavailable"
                End If
            End If
        Next
    End If
End Sub
 
Upvote 0
You should get an error doing that as cell hasn't been initialised if you put it before the loop. It needs to be:

Rich (BB code):
Private Sub Worksheet_Activate()
    Dim ThisCell2 As Range
    Range("K2").Formula = "=TODAY()"
    Range("K4:K44").ClearContents

        For Each cell In Range("J4:J44")
          If Not IsError(cell.Value) Then
            If cell = "" Then
                cell.Offset(0, 1).Value = ""
            End If
            For Each ThisCell2 In Sheet2.Range("J2:J8")
                If InStr(cell.Value, ThisCell2.Value) <> 0 Then
                    cell.Offset(0, 1).Value = "Available"
                End If
            Next ThisCell2
            For Each ThisCell2 In Sheet2.Range("K2:K21")
                If InStr(cell.Value, ThisCell2.Value) <> 0 Then
                    cell.Offset(0, 1).Value = "Unavailable"
                End If
            Next ThisCell2
          End If
        Next
        For Each cell In Range("J4:J44")
          If Not IsError(cell.Value) Then
            If cell.Offset(0, 1) = "" Then
                Set FoundRange = Sheet2.Range("J2:K22").Find(what:=cell, LookIn:=xlFormulas, lookat:=xlWhole)
                If FoundRange Is Nothing Then
                    cell.Offset(0, 1) = "Unavailable"
                End If
            End If
          End If
        Next

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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