Excel Macro - Read data from a sheet

This program read data from an Excel sheet and prints it using a message box.

 

Sub readDataFromSheet()

    '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 empName As String ' Declare a variable to store the Employee Name which will be read from the sheet.

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

        empName = Sheet1.Cells(i, 1) ' In each iteration the value in Column A is read.  The 1st iteration read from Column A, Row2.

        MsgBox (empName) ' This prints the Employee Name

    Next i

End Sub

 

Input

Enter the below data starting from A1 in Sheet1.

Name

Salary / Month

Rakesh

12000

Rohan

11230

Hrithik

15000

 

 

Output

Rakesh

Rohan

Hrithik

 

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

Comments