Skip to content

Instantly share code, notes, and snippets.

@sagarlolla
Created September 24, 2016 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagarlolla/445f2a59ce60759e7b33f8f29c964fb7 to your computer and use it in GitHub Desktop.
Save sagarlolla/445f2a59ce60759e7b33f8f29c964fb7 to your computer and use it in GitHub Desktop.
Create Excel, Open Excel, Write data in it, Read values, Search for a value
Dim objExcel
Dim objWorkBook
Dim objWorkSheet
Dim intRow
Dim intCol
Dim strPath : strPath ="D:\EmpData.xlsx"
'Create a new excel file under a given path
Set objExcel = CreateObject("Excel.Application")
Set objWorkBook = objExcel.Workbooks.Add
objWorkBook.SaveAs strPath
'Open excel file, write data in it, save and close
objExcel.Workbooks.Open strPath
Set objWorkSheet=objExcel.Worksheets("Sheet1")
objWorkSheet.Cells(1,1)= "EmpId"
objWorkSheet.Cells(1,2)= "EmpName"
objWorkSheet.Cells(1,3)= "Dept"
objWorkSheet.Cells(1,4)= "Gender"
objWorkSheet.Cells(2,1)= "786"
objWorkSheet.Cells(2,2)= "Peter"
objWorkSheet.Cells(2,3)= "ITQA"
objWorkSheet.Cells(2,4)= "M"
objExcel.ActiveWorkbook.Save
'Read values from excel workbook
intRow=objWorkSheet.UsedRange.Rows.Count
intCol=objWorkSheet.UsedRange.columns.count
For i= 1 to intRow
For j=1 to intCol
Msgbox objWorkSheet.cells(i,j).value
Next
Next
'Search for a value in excel
For each tempValue in objWorkSheet.UsedRange
If tempValue = "Peter" Then
msgbox "Search Item found"
End If
Next
objExcel.ActiveWorkbook.Close
objExcel.Quit
Set objExcel = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment