Code Help

Jimeshia

New Member
Joined
Feb 25, 2018
Messages
12
Can anyone help. I have my code below. We have to use GoTo, yeah I know, but I am needing my code to use the last x entered in to the input box (the correct one). Currently it is only using the first value in my calculation. We have to have it so that it continues to let the user choose until the user chooses in the given range. How can I correct?

Sub Calculation()


Dim i As Integer
Dim x As Variant
Dim error As Variant
Dim s0 As Double
Dim s1 As Double
Dim x1 As Variant


'Enter x in the range (0,2) as the formula is derived only for that interval


x = InputBox("Enter the value of x in the range (0,2)") 'lets user select x given the range
error = InputBox("Enter the value of Error in the range")

i = 2 'start of i
s0 = 0 'approximation of ln(x) which for the first i-1 terms
s1 = x - 1 'approximation of ln (x) for


'if user selects number outside of range, they will have to choose again


If x <= 0 Or x >= 2 Then GoTo XDirectionsNotFollowed Else GoTo Project3


XDirectionsNotFollowed:


Do Until InputBox("Enter the value of x in the range (0,2)") > 0 And InputBox("Enter the value of x in the range (0,2)") < 2


Loop


Project3:


Do While Abs(s1 - s0) > error
s0 = s1 'old becomes new
s1 = s1 + ((-1) ^ (i + 1)) * ((x - 1) ^ i) / i 'series computation
i = i + 1 'next i in series

Loop



'Customization


Application.Speech.Speak (s1)
Range("A1").Value = "User Value of x"
Range("A1").Interior.Color = vbRed
Range("B1").Value = x1
Range("A3").Value = "ln of x"
Range("A3").Interior.Color = vbRed
Range("B3").Value = "ln(x) = " & s0
Range("B3").NumberFormat = Number
Range("B3").NumberFormat = "0.0000000000000000"
Range("C1").Value = "User Error Value"
Range("C1").Interior.Color = vbRed
Range("D1").Value = error
Range("C3").Value = "Difference in Values"
Range("C3").Interior.Color = vbRed
Range("D3").Value = (Abs(s1 - s0))
Range("D3").NumberFormat = Number
Range("D3").NumberFormat = "0.0000000000000000"
Range("A:D").ColumnWidth = 30

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
it looks like you are using the value of X in a calculation before you validate it's range...

s1 = x - 1 'approximation of ln (x) for

maybe the following loop should be put near the beginning of your routine...


Code:
'if user selects number outside of range, they will have to choose again
Do While x <= 0 Or x >= 2
    x = InputBox("Enter the value of x in the range (0,2)")
Loop
Project3:

'etc
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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