Run time error 13 (type mismatch) while running macro

sarat47

New Member
Joined
Feb 10, 2013
Messages
6
Hi All,
I am getting run time error 13 i.e type mismatch in the below section (alert2) while sending email to user.



Could anyone guide me


Code:
If ((ActiveWorkbook.Worksheets(sht1).Cells(i, 14) - Date) = -5 Or (ActiveWorkbook.Worksheets(sht1).Cells(i, 14) - Date) = -3 Or _
            (ActiveWorkbook.Worksheets(sht1).Cells(i, 14) - Date) = -1) Then
               Call Fire_mail(i, "alert2")
         End If

The entire code below.
Code:
Sub MYMACRO()
Dim rc1, rc2 As Long
Dim sht1 As String
sht1 = "Master sheet"
sht2 = "Archive"
rc1 = ActiveWorkbook.Sheets(sht1).UsedRange.Rows.Count


Dim i As Long
 For i = 2 To rc1
        If ((ActiveWorkbook.Worksheets(sht1).Cells(i, 9) - Date) = -1) Then
               Call Fire_mail(i, "alert1")
         End If
        If ((ActiveWorkbook.Worksheets(sht1).Cells(i, 14) - Date) = -5 Or (ActiveWorkbook.Worksheets(sht1).Cells(i, 14) - Date) = -3 Or _
            (ActiveWorkbook.Worksheets(sht1).Cells(i, 14) - Date) = -1) Then
               Call Fire_mail(i, "alert2")
         End If
                
          Next i
       
End Sub


Private Sub Fire_mail(x As Long, str As String)
      
      Dim App As Object
      Dim item As Object
      Dim sMsgBody As String
      Dim sht1
      sht1 = "Master sheet"
      If (str = "alert1") Then
      esubject = "alert1"
        sMsgBody = "TEST1"
       ebody = sMsgBody
      
      End If
      
      If (str = "alert2") Then
      esubject = "alert2"
        sMsgBody = "test2"
        
       ebody = sMsgBody
      
      End If
      
      sendto = ActiveWorkbook.Worksheets(sht1).Cells(x, 20)
       ccto = ActiveWorkbook.Worksheets(sht1).Cells(x, 19)
       Set App = CreateObject("Outlook.Application")
       Set itm = App.CreateItem(olMailItem)
       With itm
       
         .Subject = esubject
         .To = sendto
         .CC = ccto
         .Body = ebody
         .Display
         
       End With
       Set App = Nothing
       Set itm = Nothing
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi sarat47,

Very hard to diagnose but what value is being assigned to i? I dare say it's not numeric or an error.

Robert
 
Upvote 0
Thanks Robert.

I have assigned like this:--->Dim i As Long.

Or
is there any alternative way to change the code

Requirement :- difference between date from column 9 and current date is -1, it should call 'alert1'


difference between date from column 14 and current date is -1, -3 or -5, call 'alert2'


Thanks in advance
 
Upvote 0
I have assigned like this:--->Dim i As Long.

Yes, but what is the code assigning to it? Have a look down column I and make sure the entries are numeric as this is what needs to be assigned to a long variable.
 
Upvote 0
Hi,
bit of a guess but see if this update to your code solves your problem


Code:
Sub MYMACRO()
    Dim rc1 As Long, rc2 As Long, i As Long
    Dim sht1 As Worksheet
    
    Set sht1 = ThisWorkbook.Worksheets("Master sheet")
    
    rc1 = sht1.UsedRange.Rows.Count
    
     For i = 2 To rc1
        If Val(sht1.Cells(i, 9)) - CLng(Date) = -1 Then Call Fire_mail(i, "alert1")
        Select Case Val(sht1.Cells(i, 14).Value) - CLng(Date)
            Case -5, -3, -1
               Call Fire_mail(i, "alert2")
        End Select
    Next i
       
End Sub


Dave
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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