Excel Macro - Write Data in a sheet

This program reads the monthly salary from the Excel sheet and calculates the yearly salary and writes it back to the Excel sheet.

 

Sub writeDataInSheet()

    'This program assumes that the data is present starting from the cell A1 in Sheet1.

    Dim startRow, endRow, i As Integer 'Declare the variables

    startRow = 2 'The data row actually starts from Row 2 in this example.  Row 1 is for heading.

    endRow = 4 'The data row ends at row 4.  So there are 3 rows to be read.

   

    Dim sal_month As Long

    Dim sal_year As Long

    For i = startRow To endRow ' This loop iterates from 2 to 4.

        sal_month = Sheet1.Cells(i, 2) ' Read the salary from Column B

        sal_year = sal_month * 12 ' Calculate the yearly salary

        Sheet1.Cells(i, 3) = sal_year ' Write the yearly salary in Column C

    Next i

End Sub

 

     

Input

Enter the below data starting from A1 in Sheet1.

Name

Salary / Month

Yearly Salary

Rakesh

12000

 

Rohan

11230

 

Hrithik

15000

 

 

 

Output

Name

Salary / Month

Yearly Salary

Rakesh

12000

144000

Rohan

11230

134760

Hrithik

15000

180000

 

Excel Macro | How to get started? | Hello World | Read, Write data into sheets | Record a Macro

Comments