VB.NET Formula Examples
Ready-made VB.NET formulas you can copy into the Script Editor and change for your own needs. Every example on this page was tested and gives the result shown.
Before you copy an example:
Choose VB.NET (#@VBNET) in Language.
Replace the field names with the names in your own Field List. BASIC_SALARY, GROSS_SALARY, DAYS_WORKED, PAID_HOLIDAYS, WORKING_DAYS, EMPLOYEE_AGE, EMPLOYEE_TYPE, DEPARTMENT_NAME and DESIGNATION are standard payroll fields. HRA, DA, PF, ESIC, PTAX, CONVEYANCE and the other result names stand for allowances and deductions - use the names from your company's allowance list.
The rates and limits are only examples - use your own.
Test with Script Watch before you save. See Script Editor.
1. A percentage of a field
HRA = BASIC_SALARY * 40 / 100With a basic salary of 18,000, HRA is 7200.
2. An upper or lower limit - Math.Min and Math.Max
PF = Math.Min(BASIC_SALARY, 15000) * 12 / 100Math.Min takes the smaller of the two values, so PF is worked out on at most 15,000. With a basic salary of 20,000, PF is 1800; with 12,000 it is 1440.
DA = Math.Max(GROSS_SALARY * 10 / 100, 1500)Math.Max takes the larger value, so DA is never less than 1,500. With a gross salary of 10,000, DA is 1500.
3. Based on the days worked
If WORKING_DAYS > 0 Then
CONVEYANCE = 1600 * (DAYS_WORKED + PAID_HOLIDAYS) / WORKING_DAYS
Else
CONVEYANCE = 0
End If
CONVEYANCE = Math.Round(CONVEYANCE, 0, MidpointRounding.AwayFromZero)With 22 days worked, 2 paid holidays and 26 working days, CONVEYANCE is 1477 (1600 x 24 / 26 = 1476.92, rounded).
The If WORKING_DAYS > 0 line makes sure the formula never divides by 0 - see Common mistakes below.
4. Rounding
Math.Round(1234.565, 2)
1234.56
to 2 decimals
Math.Round(2.5)
2
to the nearest even number when exactly half way
Math.Round(2.5, 0, MidpointRounding.AwayFromZero)
3
half way always goes up - the usual rounding
Math.Ceiling(1476.2)
1477
always up to a whole number
Int(1476.9)
1476
always down to a whole number
Math.Ceiling(1476.2 / 10) * 10
1480
up to the next 10
Math.Floor(1476.9 / 100) * 100
1400
down to the 100 below
For rupee amounts, use
Math.Round(value, 0, MidpointRounding.AwayFromZero)- plainMath.Round(value)rounds 2.5 down to 2.
5. Slabs - Select Case
The first Case that fits is used. With a gross salary of 12,000, PTAX is 150; with 15,000.50 it is 200.
Write the slabs from the lowest up, each with Case Is <=, so no amount falls between two slabs.
6. Two conditions together - AndAlso
AndAlso - both must be true. OrElse - one of them is enough.
A permanent employee with 25 days worked gets 500; a contract employee with 26 days gets 0.
7. No deduction above an age
At age 59, PF is 0.
8. Comparing text safely
UCase turns the text into capitals and Trim removes spaces at the ends, so " sales " still matches "SALES".
.Contains checks for a word anywhere in the text: "Area Manager" matches "MANAGER".
With a basic salary of 20,000, INCENTIVE is 1000 and SPECIAL_ALLOWANCE is 2000.
9. Several steps with your own working values
Dim declares a working value that is not a field. Working values are not saved anywhere; only fields are.
A short If can be written on one line, with no End If:
If AbsentDays < 0 Then AbsentDays = 0.With a gross salary of 26,000, 26 working days, 22 days worked and 2 paid holidays, LOP_DEDUCTION is 2000.
10. Stop early - Return
Return ends the formula at once; the fields keep the values given so far.
With no days worked, ESIC is 0. With 20 days worked and a gross salary of 18,000, ESIC is 135.
11. Your own function
A Function you write once can be used many times in the formula. It can be written before or after the lines that use it.
With a gross salary of 24,000, SPECIAL_ALLOWANCE is 1700 (7% is 1,680, rounded up to the next 50).
Common mistakes
Dividing by 0.
GROSS_SALARY / WORKING_DAYSwith 0 working days does not stop with a message - it gives a value that is not a usable number. Check first, as in examples 3 and 9./ and \ are different.
7 / 2is 3.5;7 \ 2is 3 (the decimals are dropped);7 Mod 2is 1 (the remainder).Text needs double quotes and must match capital letters exactly - use UCase as in example 8.
Every If needs an End If, unless the whole If is on one line.
Store the result:
HRA = BASIC_SALARY * 0.4, not justBASIC_SALARY * 0.4.If something does not work, press Check & Fix - it corrects the usual mistakes and lists the rest by line.