VBA code error: Run-time error '13': Type mismatch

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
Can anyone explain this error to me?

I called out where the error is down below

Code:
Sub Distribute()

Sheets("DM Material Distribution").Activate
Columns("J:MK").Select
    Selection.Delete Shift:=xlToLeft
    Range("I4").Select


Range(Cells(3, 10), Cells(1000, 1000)).ClearContents
Dim min As Date
Dim no_of_months As Integer
no_of_months = Cells(2, 9)
min = Cells(2, 7)
Cells(3, 10) = no_of_months

'***************************
'Add Months starting in 3, 10

For i = 0 To (no_of_months - 1)
    Cells(3, 10 + i) = DateAdd("m", i, min)
Next

Dim Spread_amount As Integer
Dim no_of_item_months As Integer

For x = 4 To Cells(2, 6)

    curve = Left(Cells(x, 5), 2)
    If curve = "EV" Then
    no_of_item_months = DateDiff("m", Cells(x, 7), Cells(x, 8)) + 1
    start_date_col = DateDiff("m", min, Cells(x, 7))
    
        For t = 1 To no_of_item_months
        Cells(x, t + start_date_col + 9) = Application.WorksheetFunction.RoundDown((Cells(x, 9) / no_of_item_months), 0)
        Next
        diff = Cells(x, 9) - Cells(x, start_date_col + 10) * no_of_item_months
        For i = 1 To diff
        Cells(x, i + start_date_col + 9) = Cells(x, i + start_date_col + 9) + 1
        Next
        
        
        
    ElseIf curve = "(N" Or curve = "Lo" Then

'**********************************************************************************************
'ERRORS ON THE "start_date_col = DateDiff("m", min, Cells(x, 7))"
'**********************************************************************************************
    
    Else:
    start_date_col = DateDiff("m", min, Cells(x, 7))
    Worksheets(curve).Cells(1, 1) = Cells(x, 9)
    no_of_item_months = DateDiff("m", Cells(x, 7), Cells(x, 8)) + 1
    Cells(1, 1) = no_of_item_months
    Worksheets(curve).Cells(3, 7) = no_of_item_months
    
    Dim steps As Integer
    Worksheets(curve).Cells(1, 2) = "Hello"
    Range(Worksheets(curve).Cells(4, 7), Worksheets(curve).Cells(1000, 7)).ClearContents
    Range(Worksheets(curve).Cells(4, 8), Worksheets(curve).Cells(1000, 8)).ClearContents
    Range(Worksheets(curve).Cells(4, 9), Worksheets(curve).Cells(1000, 9)).ClearContents
    steps = Worksheets(curve).Cells(3, 7)
    For i = 1 To steps
        j = (i - 1) * 12 / (steps - 1) + (steps - i) / (steps - 1)
        Worksheets(curve).Cells(3 + i, 7) = j
        Worksheets(curve).Cells(4, 5) = j
        Worksheets(curve).Cells(i + 3, 8) = Worksheets(curve).Cells(4, 6)

    Next

    Sum_all = Application.WorksheetFunction.Sum(Range(Worksheets(curve).Cells(4, 8), Worksheets(curve).Cells(1000, 8)))

    For i = 1 To steps
        Worksheets(curve).Cells(i + 3, 9) = Application.WorksheetFunction.Round(Worksheets(curve).Cells(i + 3, 8) / Sum_all * Worksheets(curve).Cells(1, 1), 0)
        Next
    Max = Worksheets(curve).Cells(1, 11)
    dif = Worksheets(curve).Cells(2, 11)
    Worksheets(curve).Cells(Max, 9) = Worksheets(curve).Cells(Max, 9) - dif
    
    start_date_col = DateDiff("m", min, Cells(x, 7))
    Cells(2, 1) = start_date_col
    
        For g = 0 To no_of_item_months - 1
            Cells(x, 10 + start_date_col + g) = Worksheets(curve).Cells(g + 4, 9)
        Next
    
    Cells(1, 1) = curve
End If
Next
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
What are the values of "min" and "Cells(x, 7)" when the error occurs?
Are you sure that cells that they are pulling from are entered as valid dates and not text?
 
Upvote 0
Solution
Thanks, Joe

A column was added that should not have been. This moved what was in column 7

Thank you
 
Upvote 0
You are welcome.

Yes, whenever you get that error, it usually means that the data it is getting is not what you are expecting.
So one of the first debugging steps is to verify what data it is actually pulling and trying to compare.
You can do that in a number of ways, such as using Message Boxes to return values, adding in error handling, or stepping through the code and hovering over the variables to see what their values are.
 
Upvote 0

Forum statistics

Threads
1,215,067
Messages
6,122,949
Members
449,095
Latest member
nmaske

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