Roderick_E
Well-known Member
- Joined
- Oct 13, 2007
- Messages
- 2,051
So I've created this cool rating UDF for my office which I thought was going to calculate per row but for some reason it repeats the calculation of whatever row I press ENTER on.
I've applied it to a column and dragged down =basiscalc()
The code should be getting the active row of each place the UDF is used...I thought. How do I fix to make it work per row? See especially actrow = ActiveCell.Row 'get active cell row
Thanks
CODE:
I've applied it to a column and dragged down =basiscalc()
The code should be getting the active row of each place the UDF is used...I thought. How do I fix to make it work per row? See especially actrow = ActiveCell.Row 'get active cell row
Thanks
CODE:
Code:
Function basiscalc(Optional calctype As String)
Application.Volatile
Dim baserate, minrate, maxrate As Double
Dim cur, basis As String
Dim actrow As Long
Dim precalc As Double
On Error Resume Next
'get columns positions
actrow = ActiveCell.Row 'get active cell row
basecol = ActiveSheet.Range("basecol").Column
basiscol = ActiveSheet.Range("basiscol").Column
curcol = ActiveSheet.Range("curcol").Column
mincol = ActiveSheet.Range("mincol").Column
maxcol = ActiveSheet.Range("maxcol").Column
baserate = Cells(actrow, basecol)
minrate = Cells(actrow, mincol)
maxrate = Cells(actrow, maxcol)
cur = Cells(actrow, curcol)
basis = Cells(actrow, basiscol)
'get totals
totalkgs = ActiveSheet.Range("totalkgs")
totallbs = ActiveSheet.Range("totallbs")
totalcbm = ActiveSheet.Range("totalcbm")
totalhbl = ActiveSheet.Range("totalhbl")
totalplt = ActiveSheet.Range("totalplt")
'do basis
precalc = baserate
'*LBS*
If basis = "lbs" Or basis = "pounds" Then
If calctype = "byrow" Then
precalc = precalc * rowlbs
Else
precalc = precalc * totallbs
End If
End If
'*KGS*
If basis = "kg" Or basis = "kgs" Or basis = "kilo" Or basis = "kilos" Or basis = "kilograms" Then
If calctype = "byrow" Then
precalc = precalc * rowkgs
Else
precalc = precalc * totalkgs
End If
End If
'*CBM*
If basis = "cbm" Or basis = "cbms" Or basis = "cubic meters" Or basis = "cubicmeters" Or basis = "m3" Then
If calctype = "byrow" Then
precalc = precalc * rowcbm
Else
precalc = precalc * totalcbm
End If
End If
'*WM*
If basis = "wm" Or basis = "w/m" Or basis = "weight/measure" Or basis = "w-m" Or basis = "wm." Then
If calctype = "byrow" Then
precalc = precalc * Application.Max(rowwm, rowkgs / 1000)
Else
precalc = precalc * Application.Max(totalwm, totalkgs / 1000)
End If
End If
'Flat
If basis = "flat" Or basis = "" Then
precalc = precalc
End If
'do min and max
precalc = Application.Max(precalc, minrate) 'minumum
If maxrate > 0 Then
precalc = Application.Min(baserate, maxrate) 'maximum
End If
basiscalc = precalc
Resume Next
End Function