How to Get Data from Excel Worksheet Using VB6
You can read and retrieve data from Microsoft Excel Worksheet easily by using your application that you made by Visual Basic 6 (VB6) programming. Simply just using its Microsoft Excel Object Library reference provided by VB6. The following code will show you how we can do that step by step. Started from how we can prepare the Excel object, get or create the Excel object, opening the Excel file, opening the Excel worksheet, accessing (read and displaying) the data from Excel file, closing the Excel worksheet, closing the Excel file, until cleaning-up the memory used by the Excel object. This is very very important for you, since almost our data stored in Microsoft Excel file, and not always in the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 'Description: Read and get data from an Excel worksheet and ' display it from the first row until the fifth row ' by using a MsgBox in Visual Basic 6. ' Including the technique how you should access Excel ' step by step properly, from opening the Excel file until ' close and clear the memory that used by Excel object. 'Author : Masino Sinaga 'Date : Sunday, August 2, 2009 'Preparation: 1. Create a new Standard exe project with 1 form ' 2. Add 1 CommandButton control on the Form1 ' 3. Add an item "Microsoft Excel X.X Object Library" ' from menu Project -> References... . X.X depends on ' version Excel or Microsoft Office you are using. ' When I tried this code, it had been installed ' Microsoft Office 2000 and Microsoft Office 2007, ' thus, X.X above is equal with 12.0 which VB6 ' displaying only the last version object library Excel. ' In this case, Microsoft Excel 12.0 Object Library ' 4. Ensure your Dataku.xls file already exists in the same ' directory where your application located, and in ' column B started from the first row until the fifth row ' you already have value in it. ' 5. Copy the following code to the Form1 editor. '------------------------------------------------------------------ Dim Excel As Excel.Application 'Excel Application Dim ExcelWBk As Excel.Workbook 'Excel Workbook Dim ExcelWS As Excel.Worksheet 'Excel Worksheet Private Sub Command1_Click() On Error GoTo Err 'Inisialitation Excel object StartExcel 'Open Dataku.xls file that located in the same directory 'where your application located. Set ExcelWBk = Excel.Workbooks.Open(App.Path & "\Dataku.xls") 'Displaying its status on the form Print "Successfully open file ..." 'Access the first Worksheet (1) 'If you want to switch to the second Worksheet, then 'simply replace (1) with (2), and so forth... Set ExcelWS = ExcelWBk.Worksheets(1) 'Displaying its status on the form Print "Successfully read Worksheet Sheet1 ..." 'Processing the ExcelWS variable With ExcelWS Dim i As Integer Dim strData As String 'Read from the first row until the fifth row For i = 1 To 5 'Assign to a variabel strData = strData & .Cells(i, 2) & vbCrLf Next i End With 'Displaying the data to MsgBox MsgBox strData 'After finished, don't forget to close the worksheet CloseWorkSheet 'Displaying its status on the form Print "Successfully close worksheet and Excel file ..." 'Don' forget neither, always clean-up the memory that 'has just been used by Excel object ClearExcelMemory 'Displaying its status on the form Print "Successfully clean-up the memory used by Excel ..." 'Displaying the message MsgBox "Finish, that's all folks ...!", vbInformation, "Good" Exit Sub Err: 'CloseWorkSheet 'When error occured, don't forget to clean-up the memory ClearExcelMemory MsgBox Err.Description, vbCritical, "Error Occured" End Sub Private Sub StartExcel() On Error GoTo Err: 'First of all, get the Excel object, and if error occured 'jumpt to the Err label on the bottom of this Sub, 'then create the Excel object. Typically, error occured here 'if the Excel object has not been created, yet Set Excel = GetObject(, "Excel.Application") Exit Sub Err: 'Create an Excel object if it does not exist. Set Excel = CreateObject("Excel.Application") End Sub Private Sub CloseWorkSheet() On Error Resume Next 'Close the Excel workbook ExcelWBk.Close 'Quit from Excel application Excel.Quit End Sub Private Sub ClearExcelMemory() 'Clean-up the memory, check first, whether 'the Excel object exists or not in the memory ... If Not ExcelWS Is Nothing Then Set ExcelWS = Nothing If Not ExcelWBk Is Nothing Then Set ExcelWBk = Nothing If Not Excel Is Nothing Then Set Excel = Nothing End Sub |
Do not forget to always cleaning-up the memory used by your Excel object. If you do not handle this, then you can not open your Excel file, until you have to first clear the memory used by your Excel object by pressing End Task button in the Task Manager dialogue box.
Print This Post
I bookmarked this link. Thank you for good job!,
You’re welcome.
Perfect work!,
Good job. It makes everybody who use the VB application happy.