• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Visual Basic 6 exporting info to Excel

I'm using VB6 and have some data, (some string labels with data, also some raw data that will eventually be graphed) and I'd like to export this to an excel spreadsheet. I'd like the operator to hit a button which somehow just transfers this data to an excel spreadsheet. Is this possible? Does anyone have a small sample chunk of code for setting it up, and running it? Do I use OLE? This is pretty urgent but I only have 95 points. Thanks
1 Solution
Éric MoreauSenior .Net ConsultantCommented:
Start a new project, add a command button to the form and a reference to Excel. Finally paste this code:

Option Explicit

Private xl As Excel.Application

Private Sub Command1_Click()
Dim intI As Integer

    Set xl = New Excel.Application
    With xl
        .Visible = True
        With .ActiveWorkbook.ActiveSheet
            .Columns("A:A").ColumnWidth = 20
            .Columns("B:B").ColumnWidth = 5
            .Columns("C:C").ColumnWidth = 15
            .Columns("C:C").NumberFormat = "#,##0.00 $"
            For intI = 1 To 100
                .Range("A" & intI).Value = intI
                .Range("C" & intI).Formula = "=SUM(A" & intI & " * 25)"
            Next intI
            .Range("C102").Formula = "=SUM(C1:C100)"
        End With
    End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
    On Error Resume Next
    xl.ActiveWorkbook.Close False
    Set xl = Nothing
End Sub
jeffreyscharpfAuthor Commented:
That's exactly what I needed to get started!



Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now