Can anybody explain this for me please?
I have a line of code which says avgval = aggval \ itemcnt in my procedure PopValues.
When, for example, aggval = 3.5 and itemcnt = 3 the result should be 1.1666
but it is showing as 1 only.
I have tried different data types (variant, single, double) but that doesn't help. In the code below you can see I have even tried Round.
Thanks!
I have a line of code which says avgval = aggval \ itemcnt in my procedure PopValues.
When, for example, aggval = 3.5 and itemcnt = 3 the result should be 1.1666
but it is showing as 1 only.
I have tried different data types (variant, single, double) but that doesn't help. In the code below you can see I have even tried Round.
Code:
Option Explicit
Dim x As Integer
Dim y As Integer
Dim i As Integer
Dim Z As Boolean
Dim itemcnt As Integer
Dim aggval As Double
Dim avgval As Double
Dim geoavg As Double
Dim prod As Double
Dim qdate As String
Dim newdate As String
Dim item As String
Dim newitem As String
Dim indbox As String
Dim price As Single
Dim gmArray(500)
Private Sub Main()
On Error Resume Next
Sheets("price_quotes").Select
Range("A2").Select
qdate = Range("A2").Value
item = Range("B2").Value
indbox = Range("F2").Value
itemcnt = 0
avgval = 0
aggval = 0
If InStr(" MNTZ", indbox) > 1 Then
price = 0
Else
price = Range("E2").Value
aggval = aggval + price
itemcnt = 1
gmArray(itemcnt) = price
End If
x = 3
y = 5
GetNewValues
Do While (newdate <> "") And (newdate <> " ")
If (newdate <> qdate) Or (newitem <> item) Then
PopValues
itemcnt = 0
aggval = 0
avgval = 0
Erase gmArray
If price <> 0 Then
itemcnt = itemcnt + 1
aggval = aggval + price
gmArray(itemcnt) = price
End If
Else
If price <> 0 Then
itemcnt = itemcnt + 1
aggval = aggval + price
gmArray(itemcnt) = price
End If
' qdate = newdate
' item = newitem
End If
qdate = newdate
item = newitem
x = x + 1
Z = 0
If x = 3174 Then Z = 1
GetNewValues
Loop
MsgBox "End of procedure"
End Sub
Private Sub GetNewValues()
Sheets("price_quotes").Select
Range("A" & x).Select
newdate = Range("A" & x).Value
newitem = Range("B" & x).Value
indbox = Range("F" & x).Value
If InStr(" MNTZ", indbox) > 1 Then
price = 0
Else
price = Range("E" & x).Value
End If
End Sub
Private Sub PopValues()
' need to move over to correct column
Sheets("Bread and Cereals_master").Select
Range("A" & y).Select
avgval = Round(aggval \ itemcnt, 4)
CalcGeoMean (gmArray)
Range("A" & y).Select
ActiveCell.Value = item
Range("B" & y).Select
ActiveCell.Value = qdate
Range("C" & y).Select
ActiveCell.Value = avgval
y = y + 1
Range("C" & y).Select
ActiveCell.Value = geoavg
Selection.Font.Bold = True
y = y + 1
End Sub
Private Sub CalcGeoMean(rs)
prod = rs(1)
If itemcnt > 500 Then MsgBox "Array out of bounds"
For i = 2 To itemcnt
prod = prod * rs(i)
Next i
geoavg = prod ^ (1 / itemcnt)
End Sub
Thanks!