?
Solved

How to move to a different worksheet when exporting a dataset to Excel

Posted on 2006-04-23
3
Medium Priority
?
292 Views
Last Modified: 2010-04-23
Hi,

I have an export that sends data from a dataset to Microsoft Excel.  It works fine but in my dataset I have 2 tables, I would like to write the data from each table to a different Worksheet in the same Excel workbook.  

I will be looping through, the first time writing the data from the first data table the second time from the second datatable.

Can anyone tell me how I can move between the worksheets please?

This is the main bit of the code I have already:

        Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object
        Dim iRow As Integer

        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add
        oSheet = oExcel.WorkSheets.Add
        oSheet = oBook.Worksheets(2)


        'SYNTAX: .Cells(Row,Column)= cellvalue
        Do Until ReportNo = 3

            'If after running the first report no records are found in the second, then exit.
            If dsIME.Tables(2).Rows.Count < 1 Then
                Exit Do
            End If

            With oSheet

                '____  TITLE INFO  ____
                .Cells(1, 1) = "INCOMPLETE TIMESHEETS: "
                .Range("A1:A2").Select()
                With oExcel.Selection.Font
                    .Name = "Verdana"
                    .ColorIndex = 5
                    .FontStyle = "Bold"
                    .Size = "12"
                End With
                '_____________________________________________________________________

            End With

            iRow = 1

            '############################################################################################################################
            'MAIN REPORT
            Dim RecordNo As Integer = 1
            Try
                Do Until RecordNo = dsIME.Tables(2).Rows.Count + 1 ' Do until there are no rows left
                    '******************&&~##~&& WRITE POINT &&~##~&&***************************
                    oSheet.Cells(RecordNo, 1) = dsIME.Tables(x).Rows(iRow).Item(0)
                    oSheet.Cells(RecordNo, 2) = dsIME.Tables(x).Rows(iRow).Item(1)
                    oSheet.Cells(RecordNo, 3) = dsIME.Tables(x).Rows(iRow).Item(2)
                    oSheet.Cells(RecordNo, 4) = dsIME.Tables(x).Rows(iRow).Item(3)
                    oSheet.Cells(RecordNo, 5) = dsIME.Tables(x).Rows(iRow).Item(4)

                    iRow = iRow + 1
                    RecordNo += 1
                Loop
                '____ end of loop

            Catch ex As Exception
                MessageBox.Show("Error from Excel report " & ex.Message)
                oExcel.Quit()
                oExcel = Nothing
            End Try

            ReportNo += 1

        Loop
0
Comment
Question by:Cherryex
  • 2
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16519298
Hi Cherryex;

Try this to seltect a worksheet:

    Excel.Application.Worksheets(WorkSheetNumber).Select

So to select worksheet 2 it would be.

    Excel.Application.Worksheets(2).Select


Fernando
0
 

Author Comment

by:Cherryex
ID: 16522953
Thank you that worked perfectly.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16525035
Glad I was able to help. :=)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question