×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Writing a value in a cel of Excel

Writing a value in a cel of Excel

Writing a value in a cel of Excel

(OP)
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

RE: Writing a value in a cel of Excel

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

RE: Writing a value in a cel of Excel

(OP)
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

RE: Writing a value in a cel of Excel

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

RE: Writing a value in a cel of Excel

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

RE: Writing a value in a cel of Excel

(OP)
Mike,

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

Greetings,

mulderm

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources