Create Excel workbook with multiple sheets?? How do I do it, can anyone help?

Last post 07-19-2008 2:46 AM by JeffSchumacher. 1 replies.

Sort Posts:

  • Create Excel workbook with multiple sheets?? How do I do it, can anyone help?

    07-18-2008, 5:42 AM
    • Loading...
    • Ferris57
    • Joined on 07-18-2008, 9:29 AM
    • Posts 1

    I can create a workbook with multiple sheets if I use a template xls file just fine using the following code:

     

    Dim oExcel As New Excel.Application

    Dim oBook As Excel.Workbook = oExcel.Workbooks.Open("C:\reports\templates\templates.xls") Dim oSheet1, oSheet2, oSheet3 As New Excel.Worksheet

     

    oSheet1 = oBook.Worksheets(1)

    oSheet2 = oBook.Worksheets(2)

    oSheet3 = oBook.Worksheets(3)

     

    oSheet1.Range("A" & "1").Value = "test"

    oSheet2.Range("B" & "2").Value = "test"

    oSheet3.Range("C" & "3").Value = "test"

     

    oBook.SaveAs("c:\test2.xls")

     

    I can also create a workbook with 1 sheet from scratch with this code:


    Dim xlApp As Excel.Application

    Dim xlWorkBook As Excel.Workbook

    Dim xlWorkSheet As Excel.Worksheet

    Dim misValue As Object = System.Reflection.Missing.Value

    xlWorkBook = xlApp.Workbooks.Add(misValue)

    xlWorkSheet = xlWorkBook.Sheets("sHEET1")

    'xlWorkSheet.Name = "tEST RUN"

    'xlWorkSheet.Cells(1, 1) = "test4"

    'xlWorkBook.SaveAs("C:\test3.xlsx")

    But I can't create a workbook with more than 1 sheet? can anyone help? I can't find an example anywhere. Any help would be greatly appreciated.

    Thanks.

    Filed under: , ,
  • Re: Create Excel workbook with multiple sheets?? How do I do it, can anyone help?

    07-19-2008, 2:46 AM
    • Loading...
    • JeffSchumacher
    • Joined on 08-04-2006, 5:31 PM
    • Riverside, CA
    • Posts 92

    You can add new worksheets to the worksheets collection using the Add method.

    See here: http://msdn.microsoft.com/en-us/library/6fczc37s.aspx

Page 1 of 1 (2 items)
Microsoft Communities
Page view counter