Source Code Show Data From Excell

download Source Code Show Data From Excell

If you can't read please download the document

description

VB6

Transcript of Source Code Show Data From Excell

Persiapan: 1. Buat satu project standard exe dengan satu form2. Tambahkan 1 control CommandButton ke atas form3. Tambahkan item "Microsoft Excel X.X Object Library" dari menu Project -> References... . X.X tergantung versi Excel atau Microsoft Office yang Anda gunakan. Saat saya mencoba code ini, di notebook saya sudah terinstall Microsoft Office 2000 dan Microsoft Office 2007, dan X.X di atas yang muncul = 12.0 di mana VB6 hanya menampilkan object library Excel yang versi terakhir. MS Excel 2007 object library versi 12.04. Pastikan file Dataku.xls sudah terdapat di dalam direktori yang sama dengan aplikasi Anda ini dan di kolom B mulai dari baris pertama sampai kelima sudah ada nilai atau datanya.5. Copy-kan code berikut ke editor form yang bertalian.Dim Excel As Excel.Application 'Excel ApplicationDim ExcelWBk As Excel.Workbook 'Excel WorkbookDim ExcelWS As Excel.Worksheet 'Excel WorksheetPrivate Sub Command1_Click()On Error GoTo Err'Inisialisasi object ExcelStartExcel'Buka file Dataku.xls yang berada di direktori yang sama'dengan aplikasi AndaSet ExcelWBk = Excel.Workbooks.Open(App.Path & "\Dataku.xls")'Tampilkan status di formPrint "Berhasil membuka file ..."'Akses Sheet pertama (1)'Jika ingin mengakses sheet kedua, ganti'(1) menjadi (2), dst...Set ExcelWS = ExcelWBk.Worksheets(1)'Tampilkan status di formPrint "Berhasil membaca Sheet1 ..."'Lakukan proses di worksheet ExcelWSWith ExcelWS Dim i As Integer Dim strData As String 'Baca mulai dari baris pertama sampai kelima For i = 1 To 5 'Tampung ke sebuah variabel strData = strData & .Cells(i, 2) & vbCrLf Next iEnd With'Tampilkan ke layarMsgBox strData'Setelah selesai, jangan lupa tutup worksheetCloseWorkSheet'Tampilkan status di formPrint "Berhasil menutup worksheet dan file Excel ..."'Jangan lupa pula, selalu bersihkan memory yang'digunakan oleh object ExcelClearExcelMemory'Tampilkan status di formPrint "Berhasil membersihkan memory Excel ..."'Tampilkan pesanMsgBox "Selesai, coy...!", vbInformation, "Mantap"Exit SubErr:'CloseWorkSheetClearExcelMemoryMsgBox Err.Description, vbCritical, "Error euy"End SubPrivate Sub StartExcel()On Error GoTo Err:'Pertama, ambil object Excel, dan jika error'lompat ke label Err di paling bawah Sub ini,'dan buat object Excel. Error terjadi jika'object Excel belum dibuatSet Excel = GetObject(, "Excel.Application")Exit SubErr:'Buat object Excel jika belum ada.Set Excel = CreateObject("Excel.Application")End SubPrivate Sub CloseWorkSheet()On Error Resume Next'Tutup Excel workbookExcelWBk.Close'Keluar dari aplikasi ExcelExcel.QuitEnd SubPrivate Sub ClearExcelMemory()'Sebelum membersihkan memory, periksa terlebih dulu'object yang akan dibersihkan...If Not ExcelWS Is Nothing Then Set ExcelWS = NothingIf Not ExcelWBk Is Nothing Then Set ExcelWBk = NothingIf Not Excel Is Nothing Then Set Excel = NothingEnd Sub