Type Mismatch Error!

ieJasonW

New Member
Joined
Mar 4, 2010
Messages
31
I can't seem to find where the type mismatch occurs. :( Any ideas?

Code:
Sub CalculateAndPlot_Click()

Dim AxialForce As Double
Dim CTWeight As Double
Dim FormationFrictionConstant As Double
Dim CasingDepth As Double
Dim CasingFrictionConstant As Double
Dim WellDepth As Double
Dim HoldAngle As Double
Dim KOP As Integer
Dim Build As Double
Dim Interval As Integer
Dim RowsForClearing As Integer
Dim WellboreFluidDensity As Double
Dim InitWellOrientation As Double
Dim theta As Double
Dim r1 As Double
Dim n As Integer
Dim i As Integer
Dim j As Integer

'This is in lb-f
AxialForce = Cells(4, 3).Value

'This is in lb/ft
CTWeight = Cells(5, 3).Value

'This is unitless
FormationFrictionConstant = Cells(7, 3).Value

'This is in ft
CasingDepth = Cells(8, 3).Value

CasingFrictionConstant = Cells(9, 3).Value

'This is in ft
WellDepth = Cells(10, 3).Value

'This is in ft
Interval = Cells(4, 13).Value

'This is in rows
RowsForClearing = Cells(5, 13).Value

'This is in lb/gal
WellboreFluidDensity = Cells(12, 3).Value

'This is in degrees. We convert to radians.
InitWellOrientation = (Cells(14, 3).Value / 180) * 3.14

'This is in ft
KOP = Cells(15, 3).Value

'This is in deg/100ft.
Build = Cells(16, 3).Value / 100

'This is in deg. We convert to radians.
HoldAngle = Cells(17, 3).Value

n = WellDepth / Interval

'clear contents of old calculations and of all formatting.
For i = 1 To (RowsForClearing)
    Cells(5 + i, 6).ClearContents
    Cells(5 + i, 7).ClearContents
    Cells(5 + i, 8).ClearContents
    Cells(5 + i, 9).ClearContents
    Cells(5 + i, 10).ClearContents
    
    Cells(5 + i, 7).Select
    Selection.Style = "Normal"
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
    End With
    
    Cells(5 + i, 8).Select
    Selection.Style = "Normal"
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
    End With
    
Next i

'format the input cells for well orientation.
For i = 0 To (n)
    
    Cells(6 + i, 7).Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    
    Cells(6 + i, 8).Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    
    Cells(6 + i, 9).Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    
    Cells(6 + i, 6).Value = i * Interval
Next i

r1 = (180 / 3.14) * (1 / Build)

Cells(6, 7).Value = Round(InitWellOrientation * 180 / 3.14, 1)

For i = 1 To WellDepth
    'Enter initial wellbore orientation for all sections shallower than the KOP
    If i < (KOP + 1) Then
        If i Mod Interval = 0 Then
            theta = InitWellOrientation * 180 / 3.14
            j = i / Interval
            Cells(6 + j, 7).Value = Round(theta, 1)
        End If
    
    'For all measured depths past the KOP...
    ElseIf i >= (KOP + 1) Then
        'Only do the calculations if it is a depth that is an output
        If i Mod Interval = 0 Then
            theta = (i - CDbl(KOP)) * 180 / 3.14 / CDbl(r1) + (InitWellOrientation * 180 / 3.14)
        End If
        
        'Ensure the max angle is the HoldAngle
        If theta > HoldAngle Then
            theta = HoldAngle
        End If
        
        'Output theta
        j = i / Interval
        Cells(6 + j, 7).Value = Round(theta, 1)
    End If
Next i

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Have you tried stepping through the code line-by-line using F8 to see where it crashes? Open the VB Editor, click anywhere inside the macro code and then press F8 to go one line at a time.
 
Upvote 0
Glad it helped.

For reference, rather than Dimming variables as "Integer", Dim them as "Long". When the code is executed those Integers are converted to Longs anyway, so save the step. (You probably won't notice increased performance in this code, but it just makes sense.)
 
Upvote 0
Good to know. I appreciate it. I learned to program in C -- and C is so particular. Anyways...

The error was because I was assigning a variable of type double to a cell whose value was a string. I just mistyped the cell's address....

ieJasonW
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,103
Members
452,302
Latest member
TaMere

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