PDA

View Full Version : VBA Project help and overflow error



Goallie11
05-05-2010, 10:28 AM
For my final project in my computer apps class we have to write a VBA program to dip a straw into a glass of water, remove the amount of water in the straw and repeat until the water level in the glass is 0.5 inches.

I'm getting overflow error 6 at the 5th line from the bottom and dont know why. This is the same code my instructor wrote during class and it worked for him.

Dim Rs As Double
Dim iRow As Integer
Dim iCol As Integer
Dim nDips As Integer
Dim Cuph As Integer
Dim Rc As Double
Dim Volc As Integer
Dim Vols As Integer
Dim Hn As Integer

'Initial Straw Radius and column number
Rs = 0.2
iCol = 1
'Clear work area
Range("A1:K2000").Clear
'iterate through straw sizes
Do While Rs <= 0.5
'initialize data
nDips = 0
iRow = 2
Cuph = 10
Cells(1, iCol) = "Rs=" & Rs
'Column Reader
Cells(iRow, iCol) = "nDips"
Cells(iRow, iCol) = "Cup Height"
iRow = iRow + 1
'write initial values
Cells(iRow, iCol) = nDips
Cells(iRow, iCol) = Cuph
iRow = iRow + 1
'iterate til final cup height is established
Do While Cuph >= 0.5
'Calculate cup volume using cup radius and water level
'Calculate straw volume using straw radius and water level
'Get new cup volume after removing the water in the straw
'Calculate new water level
Cuph = Cuph - Rs
nDips = nDips + 1
Cells(iRow, iCol) = nDips
Cells(iRow, iCol) = Cuph
iRow = iRow + 1
Loop
iCol = iCol + 2
Rs = Rs + 0.05
Loop

Bob Phillips
05-05-2010, 10:51 AM
An initial guess, change all of the Integer types to Long. Integer only holds up to 32,767, whilst longs go up to 2,147,483,647.

Goallie11
05-05-2010, 11:57 AM
Just did that and it got rid of that error but another one came up, 1004 application-defined or object defined error.

Thats in row 7 from the bottom "Cells(iRow, iCol) = nDips"

Also they way it is now it should subtract the Rs from the cupH and get a new cuph, and repeat. But it just goes on and on and on with the cuph staying at 10.

Example, it should do this

Rs = 0.2
nDips Cup Height
0 10
1 9.5


Yet it only shows Cup Height, and it remains at 10.

Goallie11
05-06-2010, 09:55 AM
Anyone have any ideas?

I really can't debug this stuff because I haven't done it long.

Bob Phillips
05-06-2010, 10:01 AM
Doi you have a workbook to try it on?

Goallie11
05-06-2010, 12:12 PM
I have the workbook that I'm writing it in, all that's in it though is the command button.

Aussiebear
05-06-2010, 12:21 PM
Then how did you test the code?

Goallie11
05-06-2010, 12:25 PM
Click the button and it's supposed to fill everything in on the worksheet. It's all contained in the code so it doesn't need outside input.

Bob Phillips
05-06-2010, 02:05 PM
Your workbook will save us recreating it all.

Goallie11
05-06-2010, 02:38 PM
The .xls file is 2.82 MB so its too big to attach. Sorry but how do I attach it now?

Goallie11
05-06-2010, 07:48 PM
Ok so I worked on the code a bit and got the numbers to line up correctly so they now display how they should:

Rs=0.2
Cup Height---nDips
10-------------0

And I added my functions, so now the code looks like this

Dim Rs As Double
Dim iRow As Integer
Dim iCol As Integer
Dim nDips As Integer
Dim Cuph As Integer
Dim Rc As Double
Dim Volc As Integer
Dim Vols As Integer
Dim Hn As Integer

'Initial Straw Radius and column number
Rs = 0.2
iCol = 1
'Clear work area
Range("A1:K2000").Clear
'iterate through straw sizes
Do While Rs <= 0.5
'initialize data
nDips = 0
iRow = 2
Cuph = 10
Cells(1, iCol) = "Rs=" & Rs
'Column Reader
Cells(iRow, iCol) = "nDips"
Cells(iRow, iCol) = "Cup Height"
iRow = iRow + 1
'write initial values
Cells(iRow, iCol) = nDips
Cells(iRow, iCol) = Cuph
iRow = iRow + 1
'iterate til final cup height is established
Do While Cuph >= 0.5
'Calculate cup volume using cup radius and water level
CupVolume
'Calculate straw volume using straw radius and water level
Straw Volume
'Get new cup volume after removing the water in the straw
New Cup
'Calculate new water level
NewWater
Cuph = Cuph - Rs
nDips = nDips + 1
Cells(iRow, iCol) = nDips
Cells(iRow, iCol) = Cuph
iRow = iRow + 1
Loop
iCol = iCol + 2
Rs = Rs + 0.05
Loop

Function CupVolume()
Volc = Pi * 16 * Cuph

End Function

Function StrawVolume()
Vols = Rs * Cuph

End Function

Function NewCup()
Volc = Volc - Vols

End Function

Function NewWater()
Cuph = (Pi * 16) / Volc

End Function

Now when it gets to that last function it does the overflow error. I know it shouldn't be going through so many iterations that it gets an error like that.

And how can I shrink the file to attach it here? Still can't figure it out.

Blade Hunter
05-06-2010, 08:17 PM
Do you have some sort of reference somewhere because I can't see where you are defining Pi.

On that if you are not your formula wil end up being 0/0 which will spit an overflow error.

Maybe put a constant reference at the top Pi = 3.141592654

Goallie11
05-06-2010, 09:08 PM
Oh i thought pi was something that it knew. I'm at the iron man premier so will change it when i get back and see if it works.

Blade Hunter
05-06-2010, 09:10 PM
Cool mate, it should fix it for you. Enjoy the movie, it is pretty damn awesome ;)

Goallie11
05-07-2010, 02:06 PM
Finally got around to trying that and it didn't work. it's still doing the error in the same place. I put

Dim Pi as Integer
Pi = 3.141592654

But still didn't work.

mdmackillop
05-07-2010, 03:20 PM
There's something here that doesn't add up!

Dim Pi as Integer
Pi = 3.141592654

mdmackillop
05-07-2010, 03:31 PM
Fix your integer types, insert your Sub and End Sub lines ensuring that the variables are available to the Functions and the code will run.

Goallie11
05-07-2010, 03:40 PM
Ok I changed Pi to long, the Sub and End Sub have been there all along, and it still gives the overflow error. Unless you mean I need to change Pi and everything else to something else.

Here's the full code:
Private Sub CommandButton1_Click()
'Name
'Student number
'Final Project

Dim Rs As Double
Dim iRow As Long
Dim iCol As Long
Dim nDips As Long
Dim Cuph As Long
Dim Rc As Long
Dim Volc As Long
Dim Vols As Long
Dim Hn As Long
Dim Pi As Long

Pi = 3.141592654

'Initial Straw Radius and column number
Rs = 0.2
iCol = 1
'Clear work area
Range("A1:K2000").Clear
'iterate through straw sizes
Do While Rs <= 0.5
'initialize data
nDips = 0
iRow = 2
Cuph = 10
Cells(1, iCol) = "Rs=" & Rs
'Column Reader
Cells(iRow, iCol + 1) = "nDips"
Cells(iRow, iCol) = "Cup Height"
iRow = iRow + 1
'write initial values
Cells(iRow, iCol + 1) = nDips
Cells(iRow, iCol) = Cuph
iRow = iRow + 1
'iterate til final cup height is established
Do While Cuph >= 0.5
'Calculate cup volume using cup radius and water level
CupVolume
'Calculate straw volume using straw radius and water level
StrawVolume
'Get new cup volume after removing the water in the straw
NewCup
'Calculate new water level
NewWater
nDips = nDips + 1
Cells(iRow, iCol + 1) = nDips
Cells(iRow, iCol) = Cuph
iRow = iRow + 1
Loop
iCol = iCol + 2
Rs = Rs + 0.05
Loop

End Sub

Function CupVolume()
Volc = Pi * 16 * Cuph

End Function

Function StrawVolume()
Vols = Rs * Cuph

End Function

Function NewCup()
Volc = Volc - Vols

End Function

Function NewWater()
Cuph = (Pi * 16) / Volc

End Function

mdmackillop
05-07-2010, 03:44 PM
Sub is in the wrong place. Step through the code to test it.
Look up the Integer and Long data types, also Single and Double.
Also look at Public Variables.

Goallie11
05-07-2010, 04:16 PM
Ok so I changed it and it goes through now, but when it gets to the 314th "dip" it displays the same number over and over until it gets error 1004 at the bolded line. 8 Rows above End Sub.

'Name
'Student number
'Final Project

Dim Rs As Double
Dim iRow As Double
Dim iCol As Integer
Dim nDips As Long
Dim Cuph As Double
Dim Rc As Integer
Dim Volc As Integer
Dim Vols As Integer
Dim Hn As Integer
Dim Pi As Double
Private Sub CommandButton1_Click()

Pi = 3.141592654

'Initial Straw Radius and column number
Rs = 0.2
iCol = 1
'Clear work area
Range("A1:K2000").Clear
'iterate through straw sizes
Do While Rs <= 0.5
'initialize data
nDips = 0
iRow = 2
Cuph = 10
Cells(1, iCol) = "Rs=" & Rs
'Column Reader
Cells(iRow, iCol + 1) = "nDips"
Cells(iRow, iCol) = "Cup Height"
iRow = iRow + 1
'write initial values
Cells(iRow, iCol + 1) = nDips
Cells(iRow, iCol) = Cuph
iRow = iRow + 1
'iterate til final cup height is established
Do While Cuph >= 0.5
'Calculate cup volume using cup radius and water level
CupVolume
'Calculate straw volume using straw radius and water level
StrawVolume
'Get new cup volume after removing the water in the straw
NewCup
'Calculate new water level
NewWater
nDips = nDips + 1
Cells(iRow, iCol + 1) = nDips
Cells(iRow, iCol) = Cuph
iRow = iRow + 1
Loop
iCol = iCol + 2
Rs = Rs + 0.05
Loop

End Sub

Function CupVolume()
Volc = Pi * 16 * Cuph

End Function

Function StrawVolume()
Vols = Rs * Cuph

End Function

Function NewCup()
Volc = Volc - Vols

End Function

Function NewWater()
Cuph = Volc / (Pi * 16)

End Function

austenr
05-07-2010, 04:58 PM
How in the world does this thing run? Never seen variables defined outside the sub they are being used in.

Goallie11
05-07-2010, 06:19 PM
I donno but it does, if someone can tell me how to upload it I will so you guys can see for yourself.

austenr
05-07-2010, 06:36 PM
PM me with your emai address and I will give you mine. You can send it to me.

Blade Hunter
05-07-2010, 09:36 PM
How in the world does this thing run? Never seen variables defined outside the sub they are being used in.

Doing that makes them available to every subroutine in the module.

Goallie11
05-07-2010, 09:55 PM
Yeah they'd all be global variables then.

Blade Hunter
05-07-2010, 10:22 PM
No, Global is available across the project, these are module level variable. If you changed DIM to GLOBAL then it would become a global variable.

Inside a sub is only available to that sub unless the variables are passed around like so:



Sub MySubHost()
Dim MyVar As String
MyVar = "Hello World"
Call MySubSlave(MyVar)
End Sub

Sub MySubSlave(MyVar As String)
MsgBox MyVar
End Sub


Alternatively you could Dim the MyVar outside the routines then it would not need passing back and forth.

Finally if you wish to have MySubSlave on a different module you would need to either pass back and forth (as above) OR Global the variable.

Historically I have been known to have a GLOBALS module in some of my larger VBA apps. Here is an example.



Option Explicit
'************************************************************************** *********************
'* *
'* MODULE: GLOBALS *
'* *
'************************************************************************** *********************

'************************************************************************** *********************
' This page is simply used to set up the global variables and define functions *
'************************************************************************** *********************
' Declare for call to mpr.dll.
Declare Function WNetGetUser Lib "mpr.dll" _
Alias "WNetGetUserA" (ByVal lpName As String, _
ByVal lpUserName As String, lpnLength As Long) As Long
Global Const NoError = 0 'The Function call was successful
Global Const RepVer As String = "1.7"
Global DatabaseUserName As String
Global DatabasePassword As String
Global ReportName As String
Global RunTimeReportID As Long
Global FuncName As String
Global Param1 As String
Global Param2 As String
Global Param3 As String
Global Param1Format As String
Global Param2Format As String
Global Param3Format As String
Global FuncReturnSTR As String
Global FuncReturn As String
Global CancelRoutine As Boolean
Global DirName As String
Global DSPListString As String
Global FormatListString As String
Global ClientModeString As String
Global SmartWarningsString As String
Global TimeInSeconds As Long
Global TimeInTime As String
Global Itemlist() As String
Global lItem As Long
Global Aggregator_Figures As Long
Global DBClient() As String
Global ConfigListVar() As String
Global ConfigListString As String
Global Debug_On As Boolean
Global DBClientList As String
Global NoArtist As Boolean
Global NoConfig As Boolean
Global CustList As Long
Global ProdList As String
Global Affiliate As String
Global ArtistString As String
Global SelectedCountry As String
Global Summary(7) As String
Global CompleteProdList() As String
Global CurrentUser As String
Global AdminReportName() As String
Global AdminProjectName() As String
Global AdminProductName() As String
Global EditAllowed As String
Global ReportOwner As String
Global MaxArtRow As Long
Global LogNum As Long
Global ProdForProj() As String
Global ProdForProjArtist() As String
Global ProdForProjTitle() As String
Global ProdForProjConfig() As String
Global ProjForProj As String
Global UnmatchedProjCode As String
Global ListBoxList() As String
Global ListBoxSQL As String
Global SalesMonthValid As Boolean
Global NewProjCodeValidate As String
Global vch_project_code As String
Global vch_name As String
Global NewProject As Boolean
Global ActiveUserRealName As String
Global n_mgr_id As Long
Global vch_artist_name As String
Global n_division As Long
Global vch_format As String
Global n_units As Long
Global vch_label_type As String
Global vch_price_point As String
Global vch_marketing_comment As String
Global vch_source As String
Global vch_signing_country As String
Global n_start_month As Long
Global ProjCreated As Boolean
Global ReturnValue As String
Global ProjCode As String
Global CurrentRepVer As String
Global GetiTunesDaily As Boolean
Global SoftChange As Boolean
Global RAProdTitle As String
Global Const BIF_RETURNONLYFSDIRS As Long = &H1
Global Const BIF_DONTGOBELOWDOMAIN As Long = &H2
Global Const BIF_RETURNFSANCESTORS As Long = &H8
Global Const BIF_BROWSEFORCOMPUTER As Long = &H1000
Global Const BIF_BROWSEFORPRINTER As Long = &H2000
Global Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Global Const MAX_PATH As Long = 260
Type BrowseInfo
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszINSTRUCTIONS As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Boolean
hNameMappings As Long
lpszProgressTitle As String
End Type
Declare Function SHGetPathFromIDListA Lib "shell32.dll" ( _
ByVal pidl As Long, _
ByVal pszBuffer As String) As Long
Declare Function SHBrowseForFolderA Lib "shell32.dll" ( _
lpBrowseInfo As BrowseInfo) As Long


Hope that explains it :).

mdmackillop
05-08-2010, 12:58 AM
Hi Goallie,
What you had orinally was almost working; at least it was producing a result, and I guided you as to how to correct the syntax errors. You have now rewritten the code to do something different, but still doesn't work.
This is your assignment and I have no interest in a right/wrong result. I don't have time to waste though, on your moving goal posts.
Good luck.
MD

Goallie11
05-08-2010, 06:47 AM
Please don't abandon me, I am trying, but when you don't have much experience doing this it takes a while. And this is what the code is supposed to do, it should have had those functions in the first place but I was just trying to debug it first.

If you can just tell me what type of declaration to use I believe that will solve it, but I've tried making just about everything a "single, double, integer or long" and it doesn't correct it. I've gone back to the original program to try making it work perfectly before putting the functions in.

Goallie11
05-08-2010, 07:33 AM
OK GREAT! I've gotten it to work! Now I just need to make some charts from it.

Thanks so much for all the help guys, I can't tell you how much I appreciate it.

mdmackillop
05-08-2010, 07:50 AM
Can you post your finished code?

Goallie11
05-08-2010, 08:17 AM
Sure, basically what I did was go back to the original after reading your post, move the sub to below the declarations, add a "+1" to the cells that read nDips, add the functions in, and change some of the declarations to double.

Dim Rs As Double
Dim iRow As Double
Dim iCol As Integer
Dim nDips As Double
Dim Cuph As Double
Dim Rc As Integer
Dim Volc As Double
Dim Vols As Double
Dim Hn As Integer
Dim Pi As Double

Private Sub CommandButton1_Click()
Pi = 3.141592654

'Initial Straw Radius and column number
Rs = 0.2
iCol = 1
'Clear work area
Range("A1:N2000").Clear
'iterate through straw sizes
Do While Rs <= 0.5
'initialize data
nDips = 0
iRow = 2
Cuph = 10
Cells(1, iCol) = "Rs=" & Rs
'Column Reader
Cells(iRow, iCol) = "nDips"
Cells(iRow, iCol + 1) = "Cup Height"
iRow = iRow + 1
'write initial values
Cells(iRow, iCol) = nDips
Cells(iRow, iCol + 1) = Cuph
iRow = iRow + 1
'iterate til final cup height is established
Do While Cuph >= 0.5
'Calculate cup volume using cup radius and water level
CupVolume
'Calculate straw volume using straw radius and water level
StrawVolume
'Get new cup volume after removing the water in the straw
NewCup
'Calculate new water level
NewWater
nDips = nDips + 1
Cells(iRow, iCol) = nDips
Cells(iRow, iCol + 1) = Cuph
iRow = iRow + 1
Loop
iCol = iCol + 2
Rs = Rs + 0.05
Loop

End Sub

Function CupVolume()
Volc = Pi * 16 * Cuph

End Function

Function StrawVolume()
Vols = Rs * Cuph

End Function

Function NewCup()
Volc = Volc - Vols

End Function

Function NewWater()
Cuph = Volc / (Pi * 16)

End Function

mdmackillop
05-08-2010, 08:56 AM
Well it look a sensible solution, unfortunately it's wrong (based on your comments)

Re your variables, it's neater and more checkable to group the types.
There is no advantage in Interger over Long, so always use Long
Single/Double may have accuracy issues. If you don't need it, go with Single. You can test the difference.
Use a Constant declaration for the likes of Pi.
You might also want to make your cup diameter a constant. It's better to get sizes etc. at the head of your code, so if you need a different calculation there is only one change. You don't need to go looking for 16 (in this case) in multiple places, if the problem changes.


Option Explicit
Dim iRow As Long
Dim iCol As Long
Dim nDips As Long
Dim Rs As Single
Dim Cuph As Single
Dim Volc As Single
Dim Vols As Single
Const pi = 3.141592654

Private Sub CommandButton1_Click()

Goallie11
05-08-2010, 09:17 AM
I copied your code and put it in place of mine and it works but it only goes to Rs = 0.45 for me, where it needs to go until Rs = 0.5

I don't think it should do that as there is still the While Rs <=0.5 loop in there. What would cause it to stop there instead of continuing?

mdmackillop
05-08-2010, 09:39 AM
Use Double, it fixes the problem. It must be some rounding error

Dim Rs As Double

Trying both produces different results for RS. Interesting.

'Single
0.2
0.25
0.25
0.3
0.3
0.35
0.35
0.4
0.4
0.45
0.45
0.5000001
'Double
0.2
0.25
0.25
0.3
0.3
0.35
0.35
0.4
0.4
0.45
0.45
0.5
0.5
0.55

Goallie11
05-08-2010, 09:58 AM
Odd... I'll change it to double later today and see how that works.

Thanks again for the help.

mdmackillop
05-08-2010, 10:01 AM
Have you found your error?

Goallie11
05-08-2010, 04:44 PM
Yeah just now changed it and it works now, goes all the way to 0.5

Yet again, I really appreciate all the help everyone.

GTO
05-08-2010, 04:54 PM
Hi Goalle11,

I realize that this is for school, so do not want to hint too broadly, but I think the mistake(s) Malcom referred to are not at the changing value of rs.

Maybe this will help. What does 16 represent?

Mark

GTO
05-08-2010, 06:00 PM
The .xls file is 2.82 MB so its too big to attach. Sorry but how do I attach it now?

By the way, I am guessing here, but if the workbook doesn't have a bunch of other stuff, I'm betting that the 2.82mb size is after running the code and the sheet is full of values. If that is the case, clear the sheet, Save and close. The wb should be down to an attachable size.

You can also zip the wb and attach a zip.

Mark

p45cal
05-09-2010, 05:27 AM
There are two overflow possibilities here:
1. Somewhere in the code you have a '+' where you should have a '-'. This is because rather than removing water from the cup you are adding water to the cup. This will inveitably lead to the situation you describe.
2. The vessel which you're emptying the straw into is smaller than the volume you need to remove from the cup.


For my final project in my computer apps class we have to write a VBA program to dip a straw into a glass of water, remove the amount of water in the straw and repeat until the water level in the glass is 0.5 inches.

I'm getting overflow error 6 at the 5th line from the bottom and dont know why. This is the same code my instructor wrote during class and it worked for him.

p45cal
05-09-2010, 05:34 AM
..moving swiftly on:
I'm guessing that the cup is cylindrical, like the straw so you need to use similar formulae to calculate the volumes of each.
Regarding your cup volume calculation:
Volc = Pi * 16 * Cuph
If you already know how to calculate the volume of a cylinder then I reckon that the 16 in the formula means the cup has a radius of 4. You have Dimmed a variable Rc, but you've neither set nor used it. You should set its value somewhere, then instead of 16 in the formula do something to Rc in there.

Now your formula for the straw's volume should be similar. Rather than
Vols = Rs * Cuph
you should see Pi in there somewhere, as well as doing a bit more with Rs.

When you've finished, both formula should look very similar, the only difference being two substitutions of lower case c for lower case s.

Your formula for the new water level
Cuph = Volc / (Pi * 16) will work fine, but only if the cup's radius is 4. Again, it might be an idea to use Rc in there, as above.

If all that's put right, you're looking for results where for a straw radius of 0.2 you expect some 1200 dips required.

Your graphs might look like:
3598

GTO
05-09-2010, 05:54 AM
...then I reckon that the 16 in the formula means the cup has a radius of 4....

Your formula for the new water level
Cuph = Volc / (Pi * 16) will work fine, but only if the cup's radius is 4....

If all that's put right, you're looking for results where for a straw radius of 0.2 you expect some 1200 dips required...

Greetings Pascal,

If I am mis-reading, I'll certainly ask forgiveness/apologize. From my current understanding, I respectfully/humbly disagree.

If the diameter is 16, the radius is 8.

The OP's functions's formula construction appear incorrect to me, as the radius is not being squared.

reference:

Function CupVolume()
Volc = Pi * 16 * Cuph

End Function

I guessed at the 16 being the radius and got 19k+ dips for the .2 straw.

Mark

p45cal
05-09-2010, 06:23 AM
If the diameter is 16, the radius is 8.Quite true.. but I didn't think 16 was the diameter, rather I guessed that 16 was supposed to be the square of something.. perhaps 4.

It was only mdmckillop who used the word diameter, Goallie11 always used radius, referring to both cup and straw, and the variable names Rs and Rc (the latter currently unused) imply radii rather than diameters.

But this has already given perhaps too much info, so to Goallie11, see here (http://www.mathsteacher.com.au/year9/ch14_measurement/18_cylinder/cylinder.htm).

mdmackillop
05-09-2010, 07:49 AM
I did use the diameter in the description, and in the context, consider it the correct figure to use as a constant. I've never heard of a cup size being measured by the radius. The calculation would of course be revised to use this new constant. As an exercise, I would leave this to the student to figure out.

Goallie11
05-09-2010, 08:08 AM
Sorry it's taken me so long to respond, I was out last night.

But to answer some questions, yeah the radius of the cup is 4, with 16 being the square. That gives the formula of Pi * 16 * Cuph for the volume of the cup.

The cup is cylindrical and so is the straw. And oh man I can't believe I missed the pi in the calculate for the straw volume! It worked so I just never looked at it. That shows you always have to be vigilant.

So I went through and changed everything and also added a new formula as we also have to graph an analytical vs numerical for Rs = 0.5 chart. This has been added at the bottom, and I believe everything works fine now.

GTO you were right, I cleared off the numbers in the workbook and it made it small enough. So it's finally attached now for everyone to see!

mdmackillop
05-09-2010, 08:54 AM
Regarding Single and Double and the 0.5 error, refer to this post (http://www.vbaexpress.com/forum/showpost.php?p=213655&postcount=3)

Goallie11
05-09-2010, 09:50 AM
What 0.5 error are you talking about? I don't want to sound ignorant but it looks to me like it works fine. The little differences in rounding don't affect this assignment too much.

GTO
05-09-2010, 10:58 AM
Quite true.. but I didn't think 16 was the diameter, rather I guessed that 16 was supposed to be the square of something.. perhaps 4.

My utter bad, you were obviously spot-on with what the OP was doing. (I suppose the absurdity of a 16 or 32 inch wide cup should have struck me harder...:p )

Goallie,

Very nice progress:thumb

Mark

mdmackillop
05-09-2010, 11:56 AM
I copied your code and put it in place of mine and it works but it only goes to Rs = 0.45 for me, where it needs to go until Rs = 0.5

I don't think it should do that as there is still the While Rs <=0.5 loop in there. What would cause it to stop there instead of continuing?

I was meaning this issue, and the reason it occurred.

Goallie11
05-09-2010, 04:53 PM
Oh yeah, changing Rs to double solved that issue.

Thanks GTO, I couldn't have done it without the help of this forum, all you guys are great.

Blade Hunter
05-09-2010, 05:03 PM
There is no advantage in Interger over Long, so always use Long

I disagree, using a long where only integer is needed is a waste of memory space. Being that the OP is learning I think it is good practice to push them towards being memory efficient.

Just because we have gigs and gigs of ram these days, doesn't mean we should be wasteful, this is why modern PC's need constant reboots and the like because of corners being cut.

p45cal
05-09-2010, 06:15 PM
It may not be as straightforward.. storage space/speed/overhead; see
here (http://www.ozgrid.com/forum/showthread.php?t=79584) and here (http://www.dailydoseofexcel.com/archives/2004/08/27/long-vs-integer/) and here (http://msdn.microsoft.com/en-us/library/aa164754) and here (http://www.eggheadcafe.com/software/aspnet/32954491/vba-code-optimization--.aspx).


I disagree, using a long where only integer is needed is a waste of memory space. Being that the OP is learning I think it is good practice to push them towards being memory efficient.

Just because we have gigs and gigs of ram these days, doesn't mean we should be wasteful, this is why modern PC's need constant reboots and the like because of corners being cut.

Blade Hunter
05-09-2010, 07:36 PM
It may not be as straightforward.. storage space/speed/overhead; see
here (http://www.ozgrid.com/forum/showthread.php?t=79584) and here (http://www.dailydoseofexcel.com/archives/2004/08/27/long-vs-integer/) and here (http://msdn.microsoft.com/en-us/library/aa164754) and here (http://www.eggheadcafe.com/software/aspnet/32954491/vba-code-optimization--.aspx).

Hmmmm, I could have sworn I already replied to this.

Thanks for the links, I have learned something :).

I stand corrected :).

Cheers

Dan