Using an Excel spreadsheet from VB


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Using an Excel spreadsheet from VB

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    Using an Excel spreadsheet from VB

    [Originally posted by Chopper]

    Can anyone tell me how to use an Excel spreadsheet that has already been created, a template so to speak. I need to insert data into certain cells of a spreadsheet. What is the syntax for something like that.

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Using an Excel spreadsheet from VB

    [Originally posted by Greg DeBacker]

    You add a Reference to the Excel Object Library to your application (Project/References menu. Then select "Microsoft Excel 8.0 Object Library")

    You then create an Excel object variable and use it to manipulate Excel.

    Option Explicit
    'object variable
    Dim ApExcel As Object

    Private Sub Form_Click()
    'Create the object
    Set ApExcel = CreateObject("Excel.application")

    ' Make Excel visible
    ApExcel.Visible = True

    'Use this to add a new workbook
    'ApExcel.Workbooks.Add

    'use this to open an existing workbook
    ApExcel.Workbooks.Open ("c:\my documents\mybook.xls")

    'Add Text to a Cell
    ApExcel.Cells(1, 1).Formula = "HELLO"

    'change the borders.
    ApExcel.Range("A1:Z1").Borders.Color = RGB(0, 0, 0)

    'adjust the column's width.
    ApExcel.Columns("A:AY").EntireColumn.AutoFit

    'select a range
    ApExcel.Range("A:Z").Select

    'change format
    ApExcel.Selection.NumberFormat = "0"

    End Sub

    Grex

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:Using an Excel spreadsheet from VB

    [Originally posted by Chopper]

    Thanks you for you help! I may need some more later on. Thanks again!!

  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Using an Excel spreadsheet from VB

    [Originally posted by Anonymous]

    Exactly what I was looking for!!! I knew there was a way to specify formats. Here is how I specified the format of a column so as not to lose leading zeroes (in this instance, column A is the SSN). This can also be used to format numeric fields/rows/columns, and probably date field formats (I haven't tried it, yet). Thanks, again!

    'select a range
    ApExcel.Range("A:A").Select
    'change format
    ApExcel.Selection.NumberFormat = "000000000"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center