Hi all,

I have a routine that uses two subs in vba to run through some iterations in excel. the code is working great but when I added a line to clear a range where a table is built each time we run the routine, I get Error: type mismatch.

FYI: the second sub "System_Analysis" is what is initially ran and inside of it, the sub "Golf" is invoked. Sorry if the order is confusing.

Sub Golf()
Dim i, x As Integer
Dim ErrorMargin, GoodMargin As Double
Dim NotClose As Boolean


'Sheets("Y").Select

i = 2
x = 0

GoodMargin = Sheets("X").Cells(132, 2).Value
ErrorMargin = GoodMargin + 1

NotClose = True

Sheets("Y").Range("H3:K121").ClearContents


Sheets("RunData").Cells(i, 1).Value = Sheets("X").Cells(130, 2).Value 'Copy initial guess to RunData Table

Do
    If Abs(ErrorMargin) > GoodMargin Then
        Sheets("RunData").Cells(i, 2).Value = Sheets("X").Cells(129, 2).Value
        Sheets("RunData").Cells(i + 1, 1).Value = Sheets("RunData").Cells(i, 2).Value
        Sheets("X").Cells(130, 2).Value = Sheets("RunData").Cells(i + 1, 1).Value
        ErrorMargin = Sheets("X").Cells(131, 2).Value
        Sheets("RunData").Cells(i, 3).Value = x
        x = x + 1
        i = i + 1
    Else
        NotClose = False
        Sheets("RunData").Cells(i, 2).Value = Sheets("X").Cells(129, 2).Value
    End If
Loop Until NotClose = False Or x > 1000
End Sub
___________________________________________________________________________________________________________________________________________________________________________________________________

Sub System_Analysis()
Dim i, j, x As Integer

'Sheets("X").Select

j = 2
i = j + 1
x = 0

Do
Sheets("X").Cells(4, 2).Value = Sheets("Y").Cells(j, 8).Value 'Grabs Flowing Pressure From Y
Sheets("X").Cells(8, 2).Value = Sheets("Y").Cells(i, 7).Value 'Grabs Deviation
Sheets("X").Cells(3, 7).Value = Sheets("Y").Cells(i, 5).Value 'Grabs Segment Length
Sheets("X").Cells(3, 5).Value = Sheets("Y").Cells(i, 2).Value

Call Golf

Sheets("Y").Cells(i, 9).Value = Sheets("X").Cells(129, 2).Value
Sheets("Y").Cells(i, 10).Value = Sheets("X").Cells(50, 2).Value
Sheets("Y").Cells(i, 11).Value = Sheets("X").Cells(50, 3).Value

Sheets("Y").Cells(i, 8).Value = Sheets("Y").Cells(j, 8).Value + Sheets("Y").Cells(i, 9).Value

j = j + 1
i = i + 1
x = x + 1
Loop Until x > 118
'dummy length of Y data as x right now, dynamically find number of iterations
End Sub
When I step through the code, the error happens after I call 'Golf' and return to system analysis to execute the next line. after reading online I think it is because the range is multiple cells and my next line sets a value for a single cell. Please help this is driving me crazy. again the code works like a charm without the .clearcontents line which is really confusing me.

Thanks and I really appreciate any help,
Capt