vba- how to use "trunc" in a vba.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
985
Office Version
  1. 2010
Platform
  1. Windows
Hello.
VBA Code:
Sub RwData(rng As Range, col As Integer)
Dim Dn As Range
For Each Dn In rng
With Application
    Dn.Offset(, -5) = .Max(Dn.Resize(, .CountA(Dn.Resize(, col)))) 'column H or 8 - is minus 5 from 13 or column M
    Dn.Offset(, -3) = .Average(Dn.Resize(, .CountA(Dn.Resize(, col))))  'this is colum J or 10 [or -3 from 13]
    Dn.Offset(, -2) = Dn - .Average(Dn.Resize(, .CountA(Dn.Resize(, col)))) 'column K or -2 from 13
End With
Next Dn
End Sub
I tried to use trunc here but do not work
VBA Code:
Dn.Offset(, -3) =.trunc( .Average(Dn.Resize(, .CountA(Dn.Resize(, col)))))
what I am doing wrong.
thank you for reading.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You do not need to call out to Excel's TRUNC function... VBA has an equivalent built-in function named Fix that works the same.
 
Upvote 0

Rick Rothstein Thanks.​

VBA Code:
Sub RwData(rng As Range, col As Integer)
Dim Dn As Range
For Each Dn In rng
With Application
   
    Dn.Offset(, -5) = .Max(Dn.Resize(, .CountA(Dn.Resize(, col)))) 'column H or 8 - is minus 5 from 13 or column M
    Dn.Offset(, -3) =.fix .Average(Dn.Resize(, .CountA(Dn.Resize(, col))))  'this is colum J or 10 [or -3 from 13]
    Dn.Offset(, -2) = Dn - .Average(Dn.Resize(, .CountA(Dn.Resize(, col)))) 'column K or -2 from 13
End With
Next Dn
End Sub
I got debug message.
1659932051621.png
 
Last edited:
Upvote 0
VBA Code:
Sub RwData(rng As Range, col As Integer)
Dim Dn As Range
For Each Dn In rng
With Application
   
    Dn.Offset(, -5) = .Max(Dn.Resize(, .CountA(Dn.Resize(, col)))) 'column H or 8 - is minus 5 from 13 or column M
    Dn.Offset(, -3) = .Fix(.Average(Dn.Resize(, .CountA(Dn.Resize(, col)))))  'this is colum J or 10 [or -3 from 13]
    Dn.Offset(, -2) = Dn - .Average(Dn.Resize(, .CountA(Dn.Resize(, col)))) 'column K or -2 from 13
End With
Next Dn
End Sub
 
Upvote 0
Fix is VBA function, not worksheet function then remove the "."
Dn.Offset(, -3) = Fix(.Average(Dn.Resize(, .CountA(Dn.Resize(, col)))))
 
Upvote 0
Thank you bebo,
now I fix the rest
VBA Code:
Sub RwData(rng As Range, col As Integer)
Dim Dn As Range
For Each Dn In rng
With Application
   
    Dn.Offset(, -5) = .Max(Dn.Resize(, .CountA(Dn.Resize(, col)))) 'column H or 8 - is minus 5 from 13 or column M
    Dn.Offset(, -3) = Fix(.Average(Dn.Resize(, .CountA(Dn.Resize(, col))))) 'this is colum J or 10 [or -3 from 13]
    Dn.Offset(, -2) = Fix(Abs(Dn - .Average(Dn.Resize(, .CountA(Dn.Resize(, col)))))) 'column K or -2 from 13
End With
Next Dn
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,716
Messages
6,174,069
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top