Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Overflow Error


I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,049
Default Overflow Error


put a break on the line and check the values of the variables.

"Bishop" wrote in message
...
I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Overflow Error


I don't get the error when I step through it. Using random sample data I
ended up with:

30/9=3.3333333333333

another random sample yielded:

27/11=2.454545454545

Do I need to restrict the output? I will be assigning the value to a label
caption and will only need it accurate to one decimal place.



"Patrick Molloy" wrote:

put a break on the line and check the values of the variables.

"Bishop" wrote in message
...
I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 82
Default Overflow Error

Could it be because varCount is 0?

And could that have something to do with ScoreBox(i) ?

Is that an array or are you trying to refer to controls on a userform
or worksheet?

On Jun 25, 7:34*pm, Bishop wrote:
I have the following code:

* * Dim varSum As Integer, varCount As Integer
* * Dim ScoringAve As Double
* * Dim i As Integer
* * For i = 1 To 18
* * * * If Me.Controls(ScoreBox(i)).Text < "" Then
* * * * * * varSum = varSum + Me.Controls(ScoreBox(i)).Text
* * * * * * varCount = varCount + 1
* * * * End If
* * Next
* * ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Overflow Error


Ok, I've tried everything. I can't figure out why this is giving me an
overflow error. I've tried every combination I can think of. Changing dim
datatypes, forcing datatypes... nothing works. I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next

.Cells(LastRow + 1, 30) = (CLng(varSum) / CLng(varCount))

How do I fix this?!

"Patrick Molloy" wrote:

put a break on the line and check the values of the variables.

"Bishop" wrote in message
...
I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Overflow Error


First, I would never use "as Integer". I'd always use "as Long". Same with "As
Single". I'd use "As Double".

Second, you never posted what was in those 18 textboxes. You could add:
Debug.print "i=" & i & " -- " & Me.Controls(ScoreBox(i)).Text
And copy from the immediate window and then paste into any followup message.

So without knowing what's in those textboxes, I'd suggest:

Dim varSum As Long, varCount As Long
Dim ScoringAve As Double
Dim i As Long
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = cdbl(varSum) / cdbl(varCount)

But I didn't test any of it. I'm sure my values wouldn't match what you're
seeing.



Bishop wrote:

I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Overflow Error


The only thing going in the textboxes are integers 1-5 (a rating like 5 stars
kind of thing)

"Dave Peterson" wrote:

First, I would never use "as Integer". I'd always use "as Long". Same with "As
Single". I'd use "As Double".

Second, you never posted what was in those 18 textboxes. You could add:
Debug.print "i=" & i & " -- " & Me.Controls(ScoreBox(i)).Text
And copy from the immediate window and then paste into any followup message.

So without knowing what's in those textboxes, I'd suggest:

Dim varSum As Long, varCount As Long
Dim ScoringAve As Double
Dim i As Long
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = cdbl(varSum) / cdbl(varCount)

But I didn't test any of it. I'm sure my values wouldn't match what you're
seeing.



Bishop wrote:

I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?


--

Dave Peterson

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default Overflow Error


I think the problem is when all of the textboxes are empty. Your suggestion
works... but only if at least one of the textboxes has a value in it.

"Dave Peterson" wrote:

First, I would never use "as Integer". I'd always use "as Long". Same with "As
Single". I'd use "As Double".

Second, you never posted what was in those 18 textboxes. You could add:
Debug.print "i=" & i & " -- " & Me.Controls(ScoreBox(i)).Text
And copy from the immediate window and then paste into any followup message.

So without knowing what's in those textboxes, I'd suggest:

Dim varSum As Long, varCount As Long
Dim ScoringAve As Double
Dim i As Long
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = cdbl(varSum) / cdbl(varCount)

But I didn't test any of it. I'm sure my values wouldn't match what you're
seeing.



Bishop wrote:

I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?


--

Dave Peterson

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 220
Default Overflow Error


Things to check:

1. You are using the ".Text" attribute of your controls. Should you be
using ".Value" instead? What type of controls are they?
2. Put in a check for varCount = 0 before the line where you calculate
ScoringAve. Something like "If (varCount = 0) Then msgBox "OMG! varCount =
0!"
3. You're doing integer math all the way until the end of the calculation
for ScoringAve. Perhaps try converting the Integers to Doubles first, i.e.
cDbl(varSum) / cDbl(varCount)
4. Check to see if your controls have a Null value in them - that can screw
up most calculations.

HTH,

Eric

"Bishop" wrote:

I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Overflow Error


I'd check to see if the values in the textboxes are really numeric:

If Me.Controls(ScoreBox(i)).Text < "" Then
if isnumeric(me.controls(Scorebox(i)).text then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
end if
End If

It could stop other typos, too.

Bishop wrote:

I think the problem is when all of the textboxes are empty. Your suggestion
works... but only if at least one of the textboxes has a value in it.

"Dave Peterson" wrote:

First, I would never use "as Integer". I'd always use "as Long". Same with "As
Single". I'd use "As Double".

Second, you never posted what was in those 18 textboxes. You could add:
Debug.print "i=" & i & " -- " & Me.Controls(ScoreBox(i)).Text
And copy from the immediate window and then paste into any followup message.

So without knowing what's in those textboxes, I'd suggest:

Dim varSum As Long, varCount As Long
Dim ScoringAve As Double
Dim i As Long
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = cdbl(varSum) / cdbl(varCount)

But I didn't test any of it. I'm sure my values wouldn't match what you're
seeing.



Bishop wrote:

I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?


--

Dave Peterson


--

Dave Peterson


  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Overflow Error


In fact...

If Me.Controls(ScoreBox(i)).Text < "" Then
if isnumeric(me.controls(Scorebox(i)).text then
varSum = varSum + cdbl(Me.Controls(ScoreBox(i)).Text)
varCount = varCount + 1
end if
End If

Dave Peterson wrote:

I'd check to see if the values in the textboxes are really numeric:

If Me.Controls(ScoreBox(i)).Text < "" Then
if isnumeric(me.controls(Scorebox(i)).text then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
end if
End If

It could stop other typos, too.

Bishop wrote:

I think the problem is when all of the textboxes are empty. Your suggestion
works... but only if at least one of the textboxes has a value in it.

"Dave Peterson" wrote:

First, I would never use "as Integer". I'd always use "as Long". Same with "As
Single". I'd use "As Double".

Second, you never posted what was in those 18 textboxes. You could add:
Debug.print "i=" & i & " -- " & Me.Controls(ScoreBox(i)).Text
And copy from the immediate window and then paste into any followup message.

So without knowing what's in those textboxes, I'd suggest:

Dim varSum As Long, varCount As Long
Dim ScoringAve As Double
Dim i As Long
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = cdbl(varSum) / cdbl(varCount)

But I didn't test any of it. I'm sure my values wouldn't match what you're
seeing.



Bishop wrote:

I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 199
Default Overflow Error


I might be wrong, but when all of the textboxes are emtpty, varCount
become 0. so, ScoringAve = (varSum / varCount) is equivalent to
ScoringAve = (varSum / 0 ). the result will end up with Overflow error.

Keiji

Bishop wrote:
I think the problem is when all of the textboxes are empty. Your suggestion
works... but only if at least one of the textboxes has a value in it.

"Dave Peterson" wrote:

First, I would never use "as Integer". I'd always use "as Long". Same with "As
Single". I'd use "As Double".

Second, you never posted what was in those 18 textboxes. You could add:
Debug.print "i=" & i & " -- " & Me.Controls(ScoreBox(i)).Text
And copy from the immediate window and then paste into any followup message.

So without knowing what's in those textboxes, I'd suggest:

Dim varSum As Long, varCount As Long
Dim ScoringAve As Double
Dim i As Long
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = cdbl(varSum) / cdbl(varCount)

But I didn't test any of it. I'm sure my values wouldn't match what you're
seeing.



Bishop wrote:
I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?

--

Dave Peterson

  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Overflow Error


And adding Keiji's response:

if varcount = 0 then
'what should happen to scoringave
else
ScoringAve = varSum) / varCount
end if

I would keep the tests for numbers, too.

Dave Peterson wrote:

In fact...

If Me.Controls(ScoreBox(i)).Text < "" Then
if isnumeric(me.controls(Scorebox(i)).text then
varSum = varSum + cdbl(Me.Controls(ScoreBox(i)).Text)
varCount = varCount + 1
end if
End If

Dave Peterson wrote:

I'd check to see if the values in the textboxes are really numeric:

If Me.Controls(ScoreBox(i)).Text < "" Then
if isnumeric(me.controls(Scorebox(i)).text then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
end if
End If

It could stop other typos, too.

Bishop wrote:

I think the problem is when all of the textboxes are empty. Your suggestion
works... but only if at least one of the textboxes has a value in it.

"Dave Peterson" wrote:

First, I would never use "as Integer". I'd always use "as Long". Same with "As
Single". I'd use "As Double".

Second, you never posted what was in those 18 textboxes. You could add:
Debug.print "i=" & i & " -- " & Me.Controls(ScoreBox(i)).Text
And copy from the immediate window and then paste into any followup message.

So without knowing what's in those textboxes, I'd suggest:

Dim varSum As Long, varCount As Long
Dim ScoringAve As Double
Dim i As Long
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = cdbl(varSum) / cdbl(varCount)

But I didn't test any of it. I'm sure my values wouldn't match what you're
seeing.



Bishop wrote:

I have the following code:

Dim varSum As Integer, varCount As Integer
Dim ScoringAve As Double
Dim i As Integer
For i = 1 To 18
If Me.Controls(ScoreBox(i)).Text < "" Then
varSum = varSum + Me.Controls(ScoreBox(i)).Text
varCount = varCount + 1
End If
Next
ScoringAve = (varSum / varCount)

Why am I getting an Overflow error for ScoringAve?

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
overflow error Brad Excel Programming 4 April 2nd 09 10:39 PM
Overflow error.. why? Fingerjob Excel Discussion (Misc queries) 4 November 13th 06 05:18 PM
Overflow error, need help mkerstei[_15_] Excel Programming 3 July 14th 06 03:10 AM
Overflow error Grd Excel Programming 1 January 21st 06 08:13 AM
overflow error ExcelMonkey[_5_] Excel Programming 6 January 22nd 04 02:34 AM


All times are GMT +1. The time now is 06:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"