Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Writing a value in a cel of Excel 1

Status
Not open for further replies.

mulderm

Electrical
Jun 4, 2003
35
NL
Hello,


In Access I have the follow VBA-code:

sub test()

Dim ex as New Excel.Application
ex.DisplayAlerts =False
ex.Workbooks.Open "c:\test.xls

ex.Range("A1").value= "Example"

ex.ActiveWorkbook.Save
ex.Quit
ex.DisplayAlerts = True
Set ex = Nothing

End Sub

If Excel is get starting and the workbook test.xls is not open
this code workt perfect.
But if test.xls is open this code do not work, the value of
cel A1 is empty.
I want to allways write the value to Cel A1 if test.xls is
open or not opened.
Can somebody helpme?

Greetings,

mulderm
 
Replies continue below

Recommended for you

Just thinking outloud here, but

Dim ex as New Excel.Application
ex.DisplayAlerts =False
ex.Workbooks.Open "c:\test.xls

if excel is not open it works because you are able to create a new excel app named test

if it is already open then you are not able to create the new app name test because it is already open

open a excel file with a different name than test ..see if the macro runs

looks like you need a if - then check to bypass creating a new file if it is running
 
How created a If- Then check for this?
Is there code to first close the open test.xls (if opened)
and then running my code?

mulderm
 
Just a bit of clarification: The line of code
Code:
Dim ex as New Excel.Application
creates a new instance of Excel. If Excel is already running, then a second instance is created. If the workbook Test.xls is open in the first instance of Excel, then opening it via your code in the second Excel instance results in it being opened read-only. Therefore, changes made will not be saved. The following procedure will detect whether
a) Excel is already running
b) Test.xls is open
and respond accordingly:
Code:
Sub OpenExcelWorkbook()
Dim XL As Excel.Application
Dim Wkb As Excel.Workbook
Dim MyWkb As Excel.Workbook
Dim TargetWorkbook As String
Dim ExcelRunning As Boolean
Dim MyWorkbookOpen As Boolean

   TargetWorkbook = "C:\Test.xls"
   
   On Error Resume Next
   Set XL = GetObject(, "Excel.Application")
   If Err.Number <> 0 Then
     Err.Clear
     ExcelRunning = False
     Set XL = CreateObject("Excel.Application")
   Else
     ExcelRunning = True
   End If
   
   MyWorkbookOpen = False
   If ExcelRunning Then
     For Each Wkb In XL.Workbooks
       If Wkb.FullName = TargetWorkbook Then
         MyWorkbookOpen = True
         Set MyWkb = Wkb
         Exit For
       End If
     Next Wkb
   End If
   
   If Not MyWorkbookOpen Then
     Set MyWkb = XL.Workbooks.Open(TargetWorkbook)
   End If
   
   MyWkb.ActiveSheet.Range("A1").Value = "Example"
   MyWkb.Save
   ' The following is optional, depending if you want to
   ' leave the workbook open if it was open already.
   If Not MyWorkbookOpen Then
     MyWkb.Close
   End If
   If Not ExcelRunning Then
     XL.Quit
   End If
   Set XL = Nothing
   
End Sub


Regards,
Mike
 
This is pretty well covered in Excel Help, including examples you can cut and paste.
 
Mike,

You´re the greatest!!!!
This code is exact what I means,works perfect.
Very, very thanks from the netherlands.

Greetings,

mulderm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top