Hello,

I have a VBA code that run for a set of data(all in numbers). The code is totally fine when it is run for the first time. But when I run it again, the error - "type mismatch" will come out. When I open a new excel workbook and paste all the data and code into it, it works at the first time. But the error will pop up again if i run for the second time. Can anyone help to solve?
Thanks a lot for the help!

My code is as below: (the red line is where the error message pop out)

Sub LinestTest2()
Dim vCoeff As Variant, vY As Variant, vX As Variant
Dim i As Variant, j As Variant, h As Variant, t As Variant
 
ReDim vY(1 To 101, 1 To 1)
ReDim vX(1 To 101, 1 To 37)

For t = 15 To 86

    For j = 1 To 101
        vY(j, 1) = Cells(179 + j, 5)
    Next
 
    For i = 1 To 101
        For h = 1 To 18
            vX(i, h) = Cells(179 + i, 8 + h)
        Next
    Next

    For i = 1 To t - 1
        vX(i, 19) = 0
    Next
    
    For i = t To 101
        vX(i, 19) = 1
    Next
    
    For i = 1 To t - 1
        For h = 20 To 37
            vX(i, h) = Cells(179 + i, 8 + h) * 0
        Next
    Next
    
    For i = t To 101
        For h = 20 To 37
            vX(i, h) = Cells(179 + i, 8 + h) * 1
        Next
    Next

    vCoeff = Application.WorksheetFunction.LinEst(vY, vX, , True)
 
    Cells(179 + t, 30) = vCoeff(3, 1)
    
    For i = 1 To 101                    'To have a blank vX to load data in next loop
        For h = 1 To 37
            vX(i, h) = 0
        Next
    Next
 
Next
 
End Sub