Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Exel file 2003 and one button A onside this exel file. when click on button A, A open dialog displayed to choose CSV file and read it. The data of CSV file was write to exel file with name KQ.xls at the same position with CSV file.Each 5 line data of CSV file was written to one Sheet of exel file and with Sheet name is KQ + i (i is increased after each 5 line was read)

I only can CSV file But i can not write to exel file.
Could you help me, please?

Thanks for advance.

Do I have to open exel application(Set ExcelApp As New Excel.Application)?
because I am working on Exel
Share this answer
E.F. Nijboer 6-Apr-11 6:25am    
Would be best next time to comment on the answer. That way the person will get an email message about it.

In answer to your question: You need an instance to an Excel COM instance. If you would look into the taskman you will see that another Excel instance is opened (and closed) during the process of writing the Excel file.
Here you have some example code and a link that might help you out:

Dim ExcelApp As Excel.Application
Dim ExcelWorkbook as Excel.Workbook
Dim ExcelSheet as Excel.Worksheet
Dim DataArray(5, nRowValues) as variant
Dim SheetIndex as Long

SheetIndex = 1

Set ExcelApp As New Excel.Application 
Set ExcelWorkbook = ExcelApp.Workbooks.Add

' Write the code that will fill the DataArray

' Write the values of the 5 lines to a new sheet
ExcelSheet = ExcelWorkbook.Worksheets.Add
ExcelSheet.Name = "KQ" & CStr(SheetIndex)
ExcelSheet.Range("A1").Value = DataArray

ExcelWorkbook.Close False, "c:\filename.xls"

Set ExcelWorkbook = Nothing
Set ExcelApp = Nothing

More info on the open dialog box:[^]

Good luck!
Share this answer
You cannot directly write to the excel file. Instead you have to open the excel file from VB6 using interop. Here are few examples how to work with the excel:

Also note that you have to add the reference to the excel object library into your project. See:[^] (this contains a small example also)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900