Writing to an Excel file 
Author Message
 Writing to an Excel file

Sorry that this is probably an easy question, but I have an array of data
which i wish to write to an existing Excel file. So far I have an array of
long values called MyArray() and :

Dim myxl As Object
Dim x As Integer

Set myxl = GetObject("c:\stats1.xls")
Set xlsheet = myxl.worksheets("Sheet1")

For x = 1 To 10
xlsheet.Cells(2 + x, 2) = Myarray(x)
Next x

but this doesn't seem to write to the file, no error message appears. Does
Excel need to be open ? If so, is there a way of doing this without Excel
being open ? Can anyone help ?

Thanks in advance

Alistair



Sat, 23 Mar 2002 03:00:00 GMT  
 Writing to an Excel file
Here's my standard Excel chit-chat program that I post here about every five
days or so.

Option Explicit
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Private Sub Command1_Click()
    xlSheet.Cells(1, 1).Value = Text1.Text
    xlSheet.Cells(2, 1).Value = Text2.Text
    xlSheet.Cells(3, 1).Value = Text3.Text
    Text4.Text = Format(CCur(Abs(xlSheet.Cells(4, 1).Value)), "##,###.00")
End Sub

Private Sub Command2_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Set xlApp = New Excel.Application
    Set xlBook = GetObject("o:\Excel Demo\book1.xls")
    Set xlSheet = xlBook.Worksheets("Sheet1")
End Sub

Private Sub Form_Unload(Cancel As Integer)
    xlBook.Close False
    xlApp.Quit
    Set xlApp = Nothing
End Sub

And yes, the Set xlApp = New Excel.Application starts Excel automatically.
This demo program writes data to cells in the Excel spreadsheet and fetches
data from cells in the Excel spreadsheet.

Eric

Quote:
> Sorry that this is probably an easy question, but I have an array of data
> which i wish to write to an existing Excel file. So far I have an array of
> long values called MyArray() and :

> Dim myxl As Object
> Dim x As Integer

> Set myxl = GetObject("c:\stats1.xls")
> Set xlsheet = myxl.worksheets("Sheet1")

> For x = 1 To 10
> xlsheet.Cells(2 + x, 2) = Myarray(x)
> Next x

> but this doesn't seem to write to the file, no error message appears. Does
> Excel need to be open ? If so, is there a way of doing this without Excel
> being open ? Can anyone help ?

> Thanks in advance

> Alistair



Sat, 23 Mar 2002 03:00:00 GMT  
 Writing to an Excel file
First of all if Excel is not running - Getobject returns nothing.  you need
to check for that and call CreateObject to start up a new Excel Object.

Unfortunately, you can not do what you want without Excel Running.

You may also need to include the Value property after cells, but I think it
is the default so you may not.

ie. - xlsheet.Cells(2 + x, 2).Value = Myarray(x)

Hope this Helps



Sun, 24 Mar 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Write to an Excel file from VB without Excel present

2. Can QBasic write and read Excel files?

3. Writing To A Excel File

4. Writing to a Excel file

5. Write and SaveAs Excel files

6. From an ASP page, is there a way write to an EXCEL file without having EXCEL installed on the IIS machine ?

7. How do I create and write to an excel file from ASP?

8. Write a Excel File Without Excel App

9. Writing Excel Files W/O Existing Excel Application

10. Writing Excel-files w/o Excel present?

11. ANNOUNCE : Platform independent toolkit for writing excel file format files

12. Can QBasic write to Excel files?

 

 
Powered by phpBB® Forum Software