How to Generate Payroll in Excel VBA (with Easy Steps)

The following dataset shows Employee Information containing the “Employee Name,” “Pay Rate,” “Total Work Time,” “Overtime Rate,” “Total Overtime,” “Health Insurance,” and “Other Deductions.” Let’s explore how to generate payroll in Excel VBA with the necessary illustrations.

Dataset for payroll in excel vba


Step 1: Insert the VBA Code into a Module

  • Go to the Developer tab >> click the Visual Basic button.

Insert the VBA Code into a Module

This opens the Visual Basic Editor in a new window.

  • Go to the Insert tab >> Select Module.

Inserting Module in VBA window

Copy the code from here and paste it into the window as shown below:

Sub Calculate_Gross_Pay()

Worksheets("VBA Code").Range("I6:I15").Formula = "=(C6*D6)+(E6*F6)"
Range("I6:I15").NumberFormat = "$#,###"

End Sub

Sub Calculate_Tax()

Worksheets("VBA Code").Range("J6:J15").Formula = "=0.2*I6"
Range("J6:J15").NumberFormat = "$#,###"

End Sub

Sub Calculate_Net_Payment()

Worksheets("VBA Code").Range("K6:K15").Formula = "=I6-(J6+G6+H6)"
Range("K6:K15").NumberFormat = "$#,###"

End Sub

Code for payroll in excel vba

Code Breakdown:

We’ll explain the 3 VBA Macros.

  • In the first macro, “Calculate_Gross_Pay”, use the Worksheet and Range objects to apply the formula to the I6:I15 range of cells located in the “VBA Code” worksheet.
  • Next, utilize the Number Format property to format the results as Currency.
  • Similarly, compute the “Tax” and “Net Payment” by executing the second and third macros.

Code explanation for payroll in excel vba


Step 2: Run the VBA Macros

  • Close the VBA window >> click the Macros button.

This opens the Macros dialog box.

  • Select the “Calculate_Gross_Pay” macro >> Press Run.

Run the VBA Macros

  • Choose the “Calculate_Tax” macro >> press Run.

  • Execute the “Calculate_Net_Payment” macro.

Executing VBA Macro

The results should appear in the animated GIF given below.

GIF showing the results for payroll in excel vba


How to Automate Payroll in Excel

Alternatively, we can automate payroll in Excel using the VLOOKUP function to pull data from a source worksheet to calculate the employee payroll. The Employee Information of the Wilbur and Company dataset shows the “Employee ID,” “Name,” “Department,” “Pay Rate,” “Total Work Time,” “Overtime Rate,” “Total Overtime,” “Health Insurance,” and “Other Deductions.”

How to Automate Payroll in Excel

Steps: 

  • Go to the C8 cell >> Move to the Data tab.
  • Insert a Data Validation drop-down by following the steps shown in the GIF.

Data Validation Steps

  • Go to cell C9 and insert the following formula:

=VLOOKUP(C8,'Employee Information'!B4:$J$14,2,FALSE)

Cell C8 refers to the “Employee ID”.

Formula Breakdown:

  • VLOOKUP(C8,’Employee Information’!B4:$J$14,2,FALSE) → looks for a value in the left-most column of a table and then returns a value in the same row from a column you specify. Here, C8 ( lookup_value argument) is mapped from the B4:J14 (table_array argument) array. Next, 2 (col_index_num argument) represents the column number of the lookup value. Lastly, FALSE (range_lookup argument) refers to the Exact match of the lookup value.
    • Output → Max

Note: Please use Absolute Cell Reference by pressing the F4 key on your keyboard.

Using VLOOKUP function

  • Use the VLOOKUP function to retrieve information related to “Max” and, in each case, specify the correct column number. For instance, the column number for “Department” is “3”, for “Pay Rate” is “4”, and so on.
  • Apply the SUM function to calculate the “Total Deductions”.

=SUM(E11:E12)

The E11 and E12 cells indicate the deductions for “Health Insurance” and “Other Deductions”.

Using SUM function

  • Enter the following formula into the C16 to obtain the “Gross Pay”:

=(C11*C12)+(C13*C14)

The C11, C12, C13, and C14 cells represent the “Pay Rate,” “Total Work Time,” “Overtime Rate,” and “Total Overtime”.

Calculating Gross Payment in payroll in excel

  • Calculate the “Tax” using the following expression.

=E13*C16

The C16 and E13 cells indicate the “Gross Payment” and “Tax Rate”.

Computing Tax in payroll in excel

  • Enter the formula into the Formula Bar to compute the Net Payment:

=C16-(C17+E14)

The C16 and C17 cells refer to the “Gross Payment” and “Tax Rate,” whereas the E14 cell represents the “Total Deductions”.

Calculating Net Payment payroll in excel

Read More: Payroll Exercises in Excel


Things to Remember

There are a few things to remember when running the VBA Code.

  • Change the worksheet name to “VBA Code” or specify the correct worksheet name.

Worksheet name for payroll in excel vba

  • Specify the appropriate range of cells where to return the calculated output.

Range of cells in payroll in excel vba


Practice Section

We have provided a practice section on the right side of each sheet so you can practice.

Practice Section


Download the Practice Workbook


<< Go Back to Formula List | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Eshrak Kader
Eshrak Kader

Eshrak Kader is a dedicated professional with a BSc. Degree in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology. He boasts a rich background in Microsoft Office Suite, and over the past year, he has authored over 120 insightful articles for the ExcelDemy. Currently, Eshrak leads a team of 5 in the ExcelDemy Video project and enjoys problem-solving and making videos on Excel. Eshrak’s passion for continuous learning underscores his commitment to excellence in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo