Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to add sheets to excel file using vb6
the following code is add a sheet but when i add more than one sheet it gives error
Dim xl As Excel.Application

Set xl = CreateObject("excel.Application")

xl.Workbooks.Open (fname)
xl.Worksheets.Add After:=Worksheets(1) ', Count:=1, Type:=xlWorksheet

code = "ASR/" & Text1.Text & "/" & Text2.Text & "/" & Text3.Text & "/" & Text4.Text
    nocode = txtnocode.Text
   heading = Text6.Text
 
    xl.Range("A1").Value = heading
 
    For i = 2 To nocode + 1
    xl.Cells(i, 1).Value = code & "/" & i - 1 '"ORG"
 Next

ActiveWorkbook.Save
Excel.ActiveWindow.Close
xl.Quit
Set xl = Nothing
MsgBox ("Done")
Posted
Updated 19-Jul-11 3:54am
v2
Comments
[no name] 19-Jul-11 10:00am    
What error?

1 solution

You need to create the worksheet object in the same manner that you created the workbook.

VB
Dim ExcelApp As Excel.Application
Dim ExcelWorkbook As Excel.Workbook
Dim ExcelSheet As Excel.Worksheet

    Set ExcelApp = Excel.Application
    Set ExcelWorkbook = ExcelApp.Workbooks.Add
    Set ExcelSheet = ExcelWorkbook.Worksheets.Add


    ExcelSheet.Name = "New_Sheet_Name " & CStr(Unique_ID)


Unique_ID: each sheet needs to be identified with a unique name.

Also assure that you dispose the objects or the Excel application will keep running after your program closes.

VB
Set ExcelSheet = Nothing
Set ExcelWorkbook = Nothing
Set ExcelApp = Nothing
 
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