Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to save multiple worksheets in excel object?
Message
 
To
28/05/2001 22:35:13
General information
Forum:
Visual Basic
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00512038
Message ID:
00512144
Views:
12
>1. I'm writing an app that can print reports in excel. I've been successful in adding worksheets and saving the workbook. The problem is, the data I wish to write is only stored in one worksheet instead of on multiple worksheets. How do I remedy this? Can anyone give me a hand in modifying the code below?
>
>2. Is there a way to check how many worksheets have been created? I noticed that when the workbook object is created it already has three worksheets. Is there a way for me to create the workbook with only one worksheet? Thanks!
>
Option Explicit

Private Sub Command1_Click()
   Dim xlShell
   Dim xlApp As Object
   Dim xlBook As Object
   Dim xlSheet As Object
    Dim nWorkSheetCtr As Integer
    Dim i As Integer
    Dim cFileName As String
    
   Set xlApp = CreateObject("Excel.Application")
   Set xlBook = xlApp.Workbooks.Add
   nWorkSheetCtr = 0
   Do Until xlBook.Worksheets.Count = 4
      xlBook.Worksheets.Add
   Loop
   
   For i = 1 To 4
      nWorkSheetCtr = nWorkSheetCtr + 1
      Set xlSheet = xlBook.Worksheets(nWorkSheetCtr)

      xlSheet.Cells(1, 1) = "TEST REPORT SYSTEM"
      xlSheet.Cells(2, 1) = "SAMPLE REPORT"
      xlSheet.Cells(3, 1) = "Run Date/Time: " & Now

      xlSheet.Cells(6, 1) = "NAME" & Trim(Str(i))
      xlSheet.Cells(6, 2) = "POSITION"
   Next i


   cFileName = "c:\temp\test.xls"
   xlBook.SaveAs cFileName
   xlBook.Close
   Set xlSheet = Nothing
   Set xlBook = Nothing
   Set xlApp = Nothing

   xlShell = Shell("C:\Program Files\Microsoft Office\Office\Excel.exe " + cFileName, vbNormalFocus)

End Sub
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform