Search This Blog

Tuesday 6 November 2012

QTP Excel Methods & Sample Programs

Create excel file and enter some data save it


  1. '###############################################  
  2. 'Create excel file and enter some data save it  
  3. '###############################################  
  4.   
  5. 'Create Excel Object    
  6. Set excel=createobject("excel.application")    
  7.     
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.     
  11. 'Add New Workbook    
  12. Set workbooks=excel.Workbooks.Add()    
  13.     
  14. 'Set the value in First row first column    
  15. excel.Cells(1,1).value="testing"    
  16.     
  17. 'Save Work Book    
  18. workbooks.saveas"D:\excel.xls"    
  19.     
  20. 'Close Work Book    
  21. workbooks.Close    
  22.     
  23. 'Quit from Excel Application    
  24. excel.Quit    
  25.     
  26. 'Release Variables    
  27. Set workbooks=Nothing    
  28. Set excel=Nothing    


Reading Values from a Specific excel Sheet

  1. '###############################################  
  2. ' Reading Values from a Specific excel Sheet  
  3. '###############################################  
  4.   
  5. 'Create Excel Object    
  6. Set excel=createobject("excel.application")    
  7.     
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open the Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Get the Control on Specific Sheet  
  15. Set worksheet1=excel.Worksheets.Item("Sheet1")  
  16.   
  17. ' Display the Values  
  18. Msgbox  worksheet1.cells(1,1).value  
  19.   
  20. 'Close Work Book    
  21. workbook.Close    
  22.     
  23. 'Quit from Excel Application    
  24. excel.Quit    
  25.     
  26. 'Release Variables    
  27. Set worksheet1=Nothing  
  28. Set workbook=Nothing  
  29. Set excel=Nothing    



Deleting Rows from Excel Sheet

  1. '###############################################  
  2. ' Deleting Rows from Excel Sheet  
  3. '###############################################  
  4.   
  5. 'Create Excel Object    
  6. Set excel=createobject("excel.application")    
  7.     
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open the Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Get the Control on Specific Sheet  
  15. Set worksheet1=excel.Worksheets.Item("Sheet1")  
  16.   
  17. 'Delete Row1  
  18. worksheet1.Rows("1:1").delete  
  19.   
  20. 'Save Excel  
  21. workbook.SaveAs("D:\excel.xls")  
  22.   
  23. 'Close Work Book    
  24. workbook.Close    
  25.     
  26. 'Quit from Excel Application    
  27. excel.Quit    
  28.     
  29. 'Release Variables    
  30. Set worksheet1=Nothing  
  31. Set workbook=Nothing  
  32. Set excel=Nothing    

Add and Delete ExcelSheet

  1. ############################################  
  2. ' Add and Delete ExcelSheet  
  3. '###############################################  
  4.   
  5. 'Create Excel Object    
  6. Set excel=createobject("excel.application")    
  7.     
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open Existing Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Add New Sheet  
  15. Set newsheet=workbook.sheets.Add  
  16.   
  17. 'Assign a Name  
  18. newsheet.name="raj"  
  19.   
  20. 'Delete Sheet  
  21. Set delsheet=workbook.Sheets("raj")  
  22. delsheet.delete  
  23.   
  24. 'Close Work Book    
  25. workbook.Close    
  26.   
  27. 'Quit from Excel Application    
  28. excel.Quit    
  29.     
  30. 'Release Variables    
  31. Set newsheet=Nothing  
  32. Set delsheet=Nothing  
  33. Set workbook=Nothing  
  34. Set excel=Nothing  

Copy an Excel Sheet of one Excel File to another Excel File

  1. '###############################################  
  2. ' Copy an Excel Sheet of one Excel File to another Excel File  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open First Excel File  
  12. Set workbook1=excel.Workbooks.Open("D:\excel1.xls")  
  13.   
  14. 'Open Second Excel File  
  15. Set workbook2=excel.Workbooks.Open("D:\excel2.xls")  
  16.   
  17. 'Copy data from first excel file sheet  
  18. workbook1.Worksheets("raj").usedrange.copy  
  19.   
  20. 'Paste Data to Second Excel File Sheet  
  21. workbook2.Worksheets("Sheet1").pastespecial  
  22.   
  23. 'Save Workbooks  
  24. workbook1.Save  
  25. workbook2.Save  
  26.   
  27. 'Close Workbooks  
  28. workbook1.Close  
  29. workbook2.Close  
  30.   
  31. 'Quit from Excel Application    
  32. excel.Quit    
  33.   
  34. 'Release Variables    
  35. Set workbook1=Nothing  
  36. Set workbook2=Nothing  
  37. Set excel=Nothing  

Comapre Two Excel Sheets Cell By Cell for a specific Range

  1. ###############################################  
  2. ' Comapre Two Excel Sheets Cell By Cell for a specific Range  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Get Control on First Sheet  
  15. Set sheet1=excel.Worksheets.Item("Sheet1")  
  16.   
  17. 'Get Control on Second Sheet  
  18. Set sheet2=excel.Worksheets.Item("Sheet2")  
  19.   
  20. 'Give the specific range for Comparision  
  21. CompareRangeStartRow=1  
  22. NoofRows2Compare=4  
  23. CompareRangeStartColumn=1  
  24. NoofColumns2Compare=4  
  25.   
  26. 'Loop through Rows  
  27. For r=CompareRangeStartRow to(CompareRangeStartRow+(NoofRows2Compare-1))  
  28.   
  29. 'Loop through columns  
  30.  For c=CompareRangeStartColumn to(CompareRangeStartColumn+(NoofColumns2Compare-1))  
  31.     
  32.   'Get Value from the First Sheet  
  33.   value1=Trim(sheet1.cells(r,c))  
  34.   'Get Value from the Second Sheet  
  35.   value2=Trim(sheet2.cells(r,c))  
  36.     
  37.   'Compare Values  
  38.   If value1<>value2 Then  
  39.     
  40.    ' If Values are not matched make the text with Red color  
  41.    sheet2.cells(r,c).font.color=vbred  
  42.      
  43.   End If  
  44.     
  45.  Next  
  46.    
  47. Next  
  48.   
  49. 'Save workbook  
  50. workbook.Save  
  51.   
  52. 'Close Work Book    
  53. workbook.Close    
  54.   
  55. 'Quit from Excel Application    
  56. excel.Quit    
  57.     
  58. 'Release Variables    
  59. Set sheet1=Nothing  
  60. Set sheet2=Nothing  
  61. Set workbook=Nothing  
  62. Set excel=Nothing  

Reading complete data from excel file

  1. ###############################################  
  2. ' Reading complete data from excel file  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open Excel File  
  12. Set workbook=excel.Workbooks.Open("D:\excel.xls")  
  13.   
  14. 'Get Control on Sheet  
  15. Set worksheet=excel.Worksheets.Item("raj")  
  16.   
  17. 'Get the count of used columns  
  18. ColumnCount=worksheet.usedrange.columns.count  
  19.   
  20. 'Get the count of used Rows  
  21. RowCount=worksheet.usedrange.rows.count  
  22.   
  23. 'Get the Starting used Row and column  
  24. top=worksheet.usedrange.row  
  25. lft=worksheet.usedrange.column  
  26.   
  27. 'Get cell object to get the values cell by cell   
  28. Set cells=worksheet.cells  
  29.   
  30. 'Loop through Rows  
  31. For row=top to (RowCount-1)  
  32.  rdata=""  
  33.  'Loop through Columns  
  34.  For col=lft to ColumnCount-1  
  35.   'Get Cell Value  
  36.   word=cells(row,col).value  
  37.     
  38.   'concatenate all row cell values into one variable  
  39.   rdata=rdata&vbtab&word  
  40.  Next  
  41.   
  42. 'Print complete Row Cell Values   
  43. print rdata  
  44. Next  
  45.   
  46. 'Close Work Book    
  47. workbook.Close    
  48.   
  49. 'Quit from Excel Application    
  50. excel.Quit    
  51.     
  52. 'Release Variables    
  53. Set worksheet=Nothing  
  54. Set workbook=Nothing  
  55. Set excel=Nothing  


Read complete data from an Excel Sheet content

  1. #############################################  
  2. ' Read complete data from an Excel Sheet content  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Open Excel File  
  12. Set workbook=excel.Workbooks.open("D:\excel.xlsx")  
  13.   
  14. 'Get Control on Sheet  
  15. Set worksheet=excel.Worksheets.Item("Sheet1")  
  16.   
  17. 'Get Used Row and Column Count  
  18. rc=worksheet.usedrange.rows.count  
  19. cc=worksheet.usedrange.columns.count  
  20.   
  21. 'Loop through Rows  
  22. For Row=1 to rc  
  23.  'Loop through Columns  
  24.  For Column=1 to cc  
  25.   'Get Cell Data  
  26.   RowData=RowData&worksheet.cells(Row,Column)&vbtab  
  27.  Next  
  28. RowData=RowData&vbcrlf  
  29. Next  
  30.   
  31. 'Display complete Data  
  32. msgbox RowData  
  33.   
  34. 'Close Work Book    
  35. workbook.Close    
  36.   
  37. 'Quit from Excel Application    
  38. excel.Quit    
  39.     
  40. 'Release Variables    
  41. Set worksheet=Nothing  
  42. Set workbook=Nothing  
  43. Set excel=Nothing  


Assign Colours to Excel Sheet Cells, Rows

  1. ###############################################  
  2. ' Assign Colours to Excel Sheet Cells, Rows  
  3. '###############################################  
  4.   
  5. 'Create Excel Object   
  6. Set excel=createobject("excel.application")  
  7.   
  8. 'Make it Visible    
  9. excel.Visible=True    
  10.   
  11. 'Add a New work book  
  12. Set workbook=excel.workbooks.add()  
  13.   
  14. 'Get the Excel Sheet  
  15. Set worksheet=excel.worksheets(1)  
  16.   
  17. 'Coloring Excell Sheet Rows  
  18. Set objrange=excel.activecell.entirerow  
  19. objrange.cells.interior.colorindex=37  
  20.   
  21. 'Coloring Excell Sheet Cell  
  22. worksheet.cells(2,1).interior.colorindex=36  
  23.   
  24. 'Save Excel  
  25. workbook.SaveAs("D:\excel.xls")  
  26.   
  27. 'Close Work Book    
  28. workbook.Close    
  29.   
  30. 'Quit from Excel Application    
  31. excel.Quit    
  32.     
  33. 'Release Variables    
  34. Set objrange=Nothing  
  35. Set worksheet=Nothing  
  36. Set workbook=Nothing  
  37. Set excel=Nothing  

1 comment: