How to display excel data in VB

epl_leicester
04-02-2003, 12:27 AM
Hi, can anybody teach me how to display the datas in Excel in the listboxes? (Any better advice to display the datas are welcome) Please reply if anybody has solution. Thankz a Million times :)

JordanChris
04-02-2003, 02:43 AM
You need a reference in your VB environment to Microsoft Excel.

Then try this code to take data from Excel back into VB. hen modify it for your needs:

Private Sub Command1()
Dim lRow As Long, lCol As Long, dValue As Double, dYear As Integer, dID As String
Dim obExcelApp As Excel.Application
Dim obWorkBook As Excel.Workbook
Dim c As Excel.Range
Set obExcelApp = CreateObject("Excel.Application")
Set obWorkBook = obExcelApp.Workbooks.Open("C:\temp\temp.xls")
For Each c In Excel.ActiveSheet.UsedRange
dValue = c.Value
MsgBox dValue ' Just show the data
Next c
Set obWorkBook = Nothing
Set obExcelApp = Nothing
End Sub

epl_leicester
04-03-2003, 01:02 AM
Hi JordanChris, thanks for your help... Since I can now load data, do you have any idea on how to add,edit and delete a data using VB on Excel? I have totally no idea and even referring to books, I had yet to learn anything from the sources...

JordanChris
04-03-2003, 02:10 AM
To put something back into Excel, you can use this code. Further details at www.MrExcel.com.

'Reference Microsoft Excel 8.0 Library Object or up
Sub Main()
Dim appExcel As Excel.Application
Set appExcel = New Excel.Application
'PURPOSE: This will turn off all dialog box
appExcel.DisplayAlerts = False
'PURPOSE: This will make it run in the background
appExcel.Visible = False
appExcel.ScreenUpdating = False
'PURPOSE: This will Open myfile.xls
appExcel.Workbooks.Open FileName:="C:\myfolder\myfile.xls"
appExcel.Worksheets("pSheet").Select
'PURPOSE: This will send data to a particular Range
appExcel.Range("A4").Value = "help me"
appExcel.Range("A5").Value = "please"
'PURPOSE: This will save workbook and close
appExcel.ActiveWorkbook.Close Savechanges:=True
'PURPOSE: This will quit Excel
appExcel.Quit
'PURPOSE: This will kill the instance
Set appExcel = Nothing
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum