hi! i wanted to know if it is possible to open an excel file in vb, access the cells and get its values. hope you can help me. i really, really need your help. thanks a lot.
Printable View
hi! i wanted to know if it is possible to open an excel file in vb, access the cells and get its values. hope you can help me. i really, really need your help. thanks a lot.
If you have excel on your computer then in VB click on Project,
Select References and check Microsoft Excel 5.0/9.0 whatever.
This is from MSDN:
CreateObject Function Example
This example uses the CreateObject function to set a reference (xlApp) to Microsoft Excel. It uses the reference to access the Visible property of Microsoft Excel, and then uses the Microsoft Excel Quit method to close it. Finally, the reference itself is
released.
PS. Make sure you set your objects=nothing, otherwise they'llCode:Dim xlApp As Object ' Declare variable to hold the reference.
Set xlApp = CreateObject("excel.application")
' You may have to set Visible property to True
' if you want to see the application.
xlApp.Visible = True
' Use xlApp to access Microsoft Excel's
' other objects.
xlApp.Quit ' When you finish, use the Quit method to close
Set xlApp = Nothing ' the application, then release the reference.
gang up on you. Hope this helps.
you could either use filesystemobject if you just want to read or use the automation method as described by johnvb6.
another method of starting excel
VB Code:
Dim objexcel As Excel.Application Dim objbook As Excel.Workbook Dm objsheet As Excel.Worksheet Set objexcel = New Excel.Application Set objbook = objexcel.Workbooks.Add Set objsheet = objexcel.Worksheets.Add
the above is to create a new instance of excel. you can access the rows and columns using objsheet.cells(row,col) where row and col are integers.
to open an existing excel file use the following
VB Code:
Set objbook = objexcel.Workbooks.Open("filename")
the codes given had really been very helpful. thanks a lot . I have another question though. Is there a way to know if the file ends or if it is the last row?