Saturday, June 18, 2011

How to Round a Number Using VBA


VBA Round Function
1. Open the Excel 2010 worksheet that contains the VBA code you want to work with. Press 'Alt' and 'F11' to launch the VBA console.
2. Click the module on the left side of the VBA console that holds the Excel macro you want to edit. Place your cursor on the code on the right side of the window, where you want to round a number.
3. Enter the following code into the VBA console:x = round(y)Where 'x' is a variable or function name in your code and 'y' is a numeric variable or an actual number. You can also place a comma after 'y' and input the number of decimal places you want to round to, so 'round(223.446, 2)' will return a value of '223.45.'
Excel Worksheet Function
4. Open the Excel 2010 worksheet that you want to work with. Press 'Alt' and 'F11' to open the VBA console.
5. Click the module that you want to work with from the left side of the VBA console. Place your cursor into the code on the right side, wherever you want to round your number.
6. Enter the following code into your VBA console:x = Application.WorksheetFunction.RoundUp(y, 0)In this code, 'x' is equal to a variable or function name and 'y' is a numeric variable or actual number. Unlike the VBA 'Round' function, the number after the 'y,' which signals the number of decimal places to round to, is not optional. Finally, you can replace 'RoundUp' with 'RoundDown' if you need to round the number down.

Blogger news