PDA

View Full Version : Solved: Continuing problem



AmardeepD
02-17-2008, 05:13 AM
Ok so continuing on from the problem i posted a couple of days earlier I have written a section of code that is giving me two problems. Firstly the code is:


Private Sub Cmdcalc_Click()
Dim ww1, bphww1, epsilon, core, ext As Double
Dim num, cellnum, filttype As Integer
num = 1

ww1 = Worksheets("Sheet1").Range("B17").Value
bphww1 = Worksheets("Sheet1").Range("B18").Value
epsilon = Worksheets("Sheet1").Range("B15").Value
filttype = Worksheets("Sheet3").Range("B2").Value

If filttype > 2 Then
core = Log(bphww1 + Sqr((bphww1 ^ 2) - 1))
For num = 1 To 15 Step 1
cellnum = 20 + (num * 2)
'higher band edge
Worksheets("Sheet1").Range("B" & cellnum).Value = 10 * Log(1 + epsilon * (Application.WorksheetFunction.Cosh((num * core) ^ 2)))
Next num
End If

num = 1
core = Log(ww1 + Sqr((ww1 ^ 2) - 1))

For num = 1 To 15 Step 1
cellnum = 19 + (num * 2)
Worksheets("Sheet1").Range("B" & cellnum).Value = 10 * Log(1 + epsilon * (Application.WorksheetFunction.Cosh((num * core) ^ 2)))
Next num

End Sub


The first problem is that even though filttype IS >2 it doesnt seem to be doing the calculations in the IF statement (or maybe just not displaying them).

The second problem confuses the problem in that it does the calculations and displays them fine right up until it comes to the last (15th) itteration when it then comes up with an error saying it cant call the Cosh function :S Needless to say this then highly confuses the first problem and points to a problem in the IF.

Thanks guys :help
(note: the maths being used in the core calculation is the mathematical basic equivalent of the Acosh function which I was asking about the other day ... i found it somewhat simpler to use)

p45cal
02-17-2008, 07:52 AM
Does the line

filttype = Worksheets("Sheet3").Range("B2").Value
mean to be:

filttype = Worksheets("Sheet1").Range("B2").Value
?
Re: unable to get the cosh property; the value it's trying to get the value of cosh for may be greater than 710 which is when my cosh function fails, both as a worksheet function on the sheet and in vba.

AmardeepD
02-17-2008, 07:54 AM
No the way I typed it is correct the variable filttype is on sheet 3

p45cal
02-17-2008, 08:17 AM
Works ok here. Have you tried putting a breakpoint early on in the macro then stepping through it using F8? Hovering over variable names in the code with the mouse pointer then allows you to check each has the value you'd expect.
Note I added a sentence re cosh on the end of my previuos note.

AmardeepD
02-18-2008, 02:40 AM
Ok thanks for that information on the limit being 710 and that works perfectly now that ive included an IF to exclude if the value is above it.

I have also managed to fix the IF filttype bit by integrating it into the second section. I have no idea why it wasnt working properly but now it works fine :S

I do have one last question. The values that are comming out as results are far too large (by a factor of 3 times for the first loop building up to over 20 times by the 8th itteration in the loop) :S. I suspect that this may be due to the units that cosh and acosh work in (i.e. either degrees or radians). If anyone could tell me what units cosh and acosh work in that would be awesome :D

p45cal
02-18-2008, 11:46 AM
I suspect it'll be radians, there is a Radians function or you could multiply/divide by pi/180.
p45cal

AmardeepD
02-19-2008, 03:30 AM
Ok that sorted it ... thanks guys!!! :bow: