Results 1 to 2 of 2

Thread: Create Excel Sheet

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374

    Post

    Hi guys, I know this has been answered here before 'cuz I have seen it here, but now I can't find it. Fitting, huh? Anyway, can someone give me an example on how to create an Excel spreadsheet from VB and copy values from a recordset into it? I would be very thankful!

    Thanks

    Andrew

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Try something like this: (Replace the Dummy Data with your Recordset Data)..
    Code:
    Private Sub Command1_Click()
        'Add a Reference to the Microsoft Excel Object Library via the "Project" Menu
        Dim oApp As New Excel.Application
        Dim oWorkbook As Excel.Workbook
        Dim oSheet As Excel.Worksheet
        Dim iRow As Long
        Dim iCol As Long
        
        Set oWorkbook = oApp.Workbooks.Add
        Set oSheet = oWorkbook.Sheets(1)
        With oSheet
            For iRow = 1 To 10
                For iCol = 1 To 10
                    .Cells(iRow, iCol) = Rnd * 1000
                Next
            Next
            .SaveAs "C:\NewSheet.xls"
        End With
        oWorkbook.Close
        oApp.Quit
        Set oSheet = Nothing
        Set oWorkbook = Nothing
        Set oApp = Nothing
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width