how to access data from excel to/from another program using Vb

  • Thread starter Thread starter Abel
  • Start date Start date
Dear Abel

Try below code after referencing Microsoft Excel Object Library

If this post helps click Yes
---------------
Jacob Skaria


Dim xlApp As Excel.Application
Dim xlWB1 As Excel.Workbook

Set xlWB1 = xlApp.OpenWorkbook("c:\temp.xls")
'Read values
strTemp = xlWB1.Sheets(1).Range("A1")
xlWB1.Close
Set xlWB1 = Nothing

xlApp.Quit
Set xlApp = Nothing
 
Let's say you are in Access, for instance, and you want to control Excel.
Follow the logic below:
It is quite easy to perform operations in Excel, and control the entire
process from Access. Make sure you set a reference to Excel, and then run
this code in an Access module:
Option Compare Database

Option Explicit ' Use this to make sure your variables are defined

' One way to be able to use these objects throughout the Module is to
Declare them here, and not in a Sub
Private objExcel As Excel.Application
Private xlWB As Excel.Workbook
Private xlWS As Excel.Worksheet

Sub Rep()
Dim strFile As String

strFile = "C:\filename.xls"

' Of course, this is just an example; put the actual path to your actual
file here…

' Opens Excel and makes it Visible

Set objExcel = New Excel.Application

objExcel.Visible = True

' Opens up a Workbook

Set xlWB = objExcel.Workbooks.Open(strFile)

' Sets the Workseet to the last active sheet - Better to use the commented
version and use the name of the sheet.

Set xlWS = xlWB.ActiveSheet

' Set xlWS = xlWB("Sheet1")

With xlWS ' You are now working with the Named file and the named worksheet

' Your Excel code begins here…you can even record a macro and make the
process super easy!!

End With

' Close and Cleanup

xlWB.SaveAs xlSaveFile
xlWB.Close
xlapp.Quit

Set xlapp = Nothing

End Sub

Send me an email if you have any questions about controlling Excel from
Access!!

Regards,
Ryan---
 
Back
Top