Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking VB TO EXCEL 2

Status
Not open for further replies.

tweea

Computer
Mar 29, 2005
4
I just started using visual basic and I want to know how to link Excel with visual basic. I created the interface already...I jus need help wit the coding.
 
Replies continue below

Recommended for you

Usually you transfer data from excel spreadsheet into VB and values you get in VB variables back into excel spreadsheet- somewhere you have to start.
M777182
 
The easy way is to use GetObject or CreateObject (depends if Excel is already running or not). To open, Dim myXL at form or global level as required then use something like:
Code:
On Error Resume Next 'ignore errors
 Set myXL = GetObject(, "Excel.Application") 'look for a running copy
 If Err.Number <> 0 Then 'If Excel is not running then
  Set myXL = CreateObject("Excel.Application") 'run it
 End If
 Err.Clear   ' Clear Err object in case error occurred.
 On Error GoTo 0 'Resume normal error processing
 myXL.Workbooks.Open ("c:\j3.xls") ' Your full path
 myXL.Visible = True ' or False if you're just processing
 myXL.Worksheets(1).Range("J1").Value = "fred" 'set value
 lngInput = myXL.Worksheets(1).Range("H2").Value ' get

To close when you're finished:
Code:
Dim W As Workbook
  For Each W In Workbooks
    W.Close savechanges:=False ' or True as reqd
  Next W
myXL.Quit
Set myXL = Nothing

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting

UK steam enthusiasts:
 
Thanks 4 the help..i'll give it a try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor