# VBA Troubleshooting

**Question description**

I need someone to trouble shoot this code. The button needs to be automated. I think the idea of the button is to initiate the code with one click. There are also some problems with the code and it doesn't run. If someone could help troubleshoot that it would be appreciated

PLEASE ask me if there is anything unclear in the code or anything like that. I realize it's a little complicated since it's for a drilling engineering class but I can clarify if need be. Ask and I will get back to you within an hour

**Code is pasted below. I will email my spreadsheet once I am set on a tutor**

------------

Option Explicit

Function log10(x As Double) As Double

log10 = log(x) / log(10)

End Function

Function PLossInside_f(Q As Double, ID As Double) As Double

Dim MW As Double, A As Double, B As Double, C As Double

MW = Worksheets("Mud Data").Cells(2, 2).Value

A = Worksheets("Mud Data").Cells(5, 6).Value

B = Worksheets("Mud Data").Cells(6, 6).Value

C = Worksheets("Mud Data").Cells(7, 6).Value

Dim n As Double, R As Double, T_guess As Double, K_v As Double, x As Double

Dim delP As Double, T_new As Double, Tol As Double

Dim f_p As Double, k_p As Double

Dim v As Double, N_p As Double, AA As Double, L As Double, BB As Double, Y As Double, fric As Double

Dim diff As Double, i As Integer, h As Double, k As Double

R = 25

n = B + 2 * C * log(R)

T_guess = Exp(A + B * log(R) + C * log(R) ^ 2)

Tol = 0.0005

diff = 1

i = 1

Do While (diff > Tol) And (i < 30)

h = 1 / (2 * C)

k = ((B ^ 2) + (4 * C) * (log(T_guess) - A)) ^ (1 / 2)

R = Exp(h * ((-B) k))

n = B + 2 * C * log(R)

x = (0.123 / n) / (1 - (1.0678) ^ ((-2) / n))

K_v = 0.01066 * T_guess / (1.703 * R) ^ n

f_p = ((3 * n + 1) / (4 * n * x)) ^ n

k_p = f_p * K_v

v = Q / (2.45 * (ID ^ 2))

N_p = (1.86 / k_p) * ((ID / 96) ^ n) * (v ^ (2 - n)) * MW

AA = (log10(n) + 3.93) / 50

L = 3470 - 1370 * n

BB = (1.75 - log10(n)) / 7

Y = 4270 - 1370 * n

If (N_p <= L) Then

fric = 16 / N_p

'Shows laminar flow regime

ElseIf (N_p >= Y) Then

fric = AA / N_p ^ BB

'Shows turbulent flow regime

Else

fric = ((N_p - L) / 800) * (AA / Y ^ BB - 16 / L) + 16 / L

End If

delP = (fric * MW * v ^ 2) / (25.8 * ID)

T_new = 281.4 * delP * ID

diff = Abs(T_new - T_guess)

T_guess = T_new

i = 1 + i

Loop

PLossInside_f = delP

End Function

Function PLossAnnular_f(Q As Double, OD As Double, HD As Double) As Double

Dim MW As Double, A As Double, B As Double, C As Double

MW = Worksheets("Mud Data").Cells(2, 2).Value

A = Worksheets("Mud Data").Cells(5, 6).Value

B = Worksheets("Mud Data").Cells(6, 6).Value

C = Worksheets("Mud Data").Cells(7, 6).Value

Dim n As Double, R As Double, T_guess As Double, K_v As Double, x As Double, T_new As Double, Tol As Double

Dim v As Double, N_a As Double, AA As Double, BB As Double, L As Double, Y As Double, delP As Double

Dim fric As Double, diff As Double, i As Integer, h As Double, k As Double, f_a As Double, k_a As Double

R = 25

n = B + 2 * C * log(R)

T_guess = Exp(A + B * log(R) + C * log(R) ^ 2)

Tol = 0.0005

diff = 1

i = 1

Do While (diff > Tol) And (i < 30)

h = 1 / (2 * C)

k = ((B ^ 2) + (4 * C) * (log(T_guess) - A)) ^ (1 / 2)

R = Exp(h * ((-B) + k))

n = B + 2 * C * log(R)

x = (0.123 / n) / (1 - (1.0678) ^ ((-2) / n))

K_v = 0.01066 * T_guess / (1.703 * R) ^ n

f_a = ((2 * n + 1) / (3 * n * x)) ^ n

k_a = f_a * K_v

v = Q / (2.45 * (HD ^ 2 - OD ^ 2))

N_a = (2.79 / k_a) * (((HD - OD) / 144) ^ n) * (v ^ (2 - n)) * MW

AA = (log10(n) + 3.93) / 50

L = 3470 - 1370 * n

BB = (1.75 - log10(n)) / 7

Y = 4270 - 1370 * n

If (N_a <= L) Then

fric = 24 / N_a

'shows laminar flow regime

ElseIf (N_a >= Y) Then

fric = AA / N_a ^ BB

'shows turbulent flow regime

Else

fric = ((N_a - L) / 800) * (AA / Y ^ BB - 24 / L) + 24 / L

End If

delP = (fric * MW * v ^ 2) / (25.8 * (HD - OD))

T_new = 281.4 * (HD - OD) * delP

diff = Abs(T_new - T_guess)

T_guess = T_new

i = 1 + i

Loop

PLossAnnular_f = delP

End Function

amery_311project3.xlsm

## Tutor Answer

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors