|
Visual Basic 6 (VB6) Code to Put Data in an Excel Spreadsheet |
|
|
|
|
Written by Michael Salsbury
|
|
Wednesday, 15 June 2005 |
|
The following Visual Basic 6.0 (VB6) subroutine will show you how to write data into Microsoft Excel spreadsheet cells from your program. As with all my sample code, it's provided as-is without warranty or support. Use at your own risk.
Private Sub LoadExcel() ' Create the objects needed Dim excelApp As Object Dim excelWorkbook As Object Dim excelSheet As Object ' ' Create an instance of Microsoft Excel ' Set excelApp = CreateObject("Excel.application") ' ' Make Excel visible to the user. ' excelApp.Visible = True ' ' Add a new Workbook to Excel ' Set excelWorkbook = excelApp.workbooks.Add ' ' Select the worksheet ' Set excelSheet = excelWorkbook.worksheets(1) ' ' Fill in some values ' excelSheet.range("A1:A5").numberformat = "@" excelSheet.range("A1").Value = "@" excelSheet.range("A2").Value = "mar25" excelSheet.range("A3").Value = "sep21" excelSheet.range("A4").Value = "abc12" excelSheet.range("A5").Value = "xxx99" excelSheet.range("B1:A4").numberformat = "General" excelSheet.range("B1").Value = "General" excelSheet.range("B2").Value = "mar25" excelSheet.range("B3").Value = "sep21" excelSheet.range("B4").Value = "abc12" excelSheet.range("B5").Value = "xxx99" ' ' Save the workbook as c:\test.xls ' excelWorkbook.saveas ("c:\test.xls") ' ' Close the workbook and quit Excel ' excelWorkbook.Close excelApp.quit ' ' Get rid of the objects we've created. ' Set excelSheet = Nothing Set excelWorkbook = Nothing Set excelApp = Nothing End Sub
The following text is not intended for the human readers of this page, but rather for electronic readers who are looking for lots of specific words to be here to define and categorize this page: This page contains a sample subroutine written in Microsoft Visual Basic 6.0 to place some sample data into an Excel spreadsheet. It shows how using certain techniques to place that data into a spreadsheet will result in garbled data being stored by Excel, as Excel tries to "understand" the data you're inputting. It also shows an alternate input method that essentially tells Excel that it shouldn't try to interpret the input. Sample VB or VB6 code to put data into Excel. Example source code to place data in Excel from VB6 or rather Visual Basic 6.0. Yes, this sample subroutine shows code that will allow your Visual Basic (VB) program to put some data into Microsoft Excel spreadsheets. Again, for those non-human readers, free code to input data into an Excel spreadsheet from VB6 (Microsoft Visual Basic 6.0).
Related Blogs:
Related Links:
|
|
Last Updated ( Friday, 24 March 2006 )
|