VB and Excel 2000

Posted on 2005-04-28
Last Modified: 2010-05-02

I need to write some code to essentially do the following:

Open an excel file in VB6

Traverse 2 sheets, taking data from specified cells and loading them into an excel object within vb6

As above, but write / format cells

Save the excel file.

Never really used Excel object model before and it seems a little tough to get to grips with.

Any url's highlighting examples of the above or snippets of code will be much appreciated.

Question by:raybetts
    LVL 29

    Expert Comment

    Esablish a reference to the Microsoft Excel object then:

    Dim xl As Excel.Application
    Dim wkbSource As Excel.Workbook
    Dim wshSource As Excel.Worksheet
    Dim rngSource As Excel.Range
    Set xl = New Excel.Application
    xl.Visible = True
    Set wkbSource = wl.Workbooks.Open(strPath & strFile)
    Set wshSource = wkbSource.Worksheets("Sheet1")
    Set rngSource = wshSource.Range("A1")

    ' And so on
    'Don't forget to close all objects the you are done.  If you need help with syntax, you can use the Macro recorder in Excel then copy the code into VB and modify it.


    LVL 1

    Author Comment

    Thanks leon, that helps a lot.

    The only other thing I cannot figure out is this:

    Last populated column
    Last populated row

    So that I can loop till last col and row and take the data into a db.

    If you know this, then you have the points :-)
    LVL 29

    Accepted Solution

    That you need to do is find the last cell on the sheet:

    Dim rngLastCell As Excel.Range, intLastRow As integer, intLastCol As Integer

    Set rngLastCell = wshSource.Cells.SpecialCells(xlCellTypeLastCell)

    intLastRow = rngLastCell.Row
    intLastCol = rngLastCell.Column

    LVL 29

    Expert Comment

    Thanks for the grade,


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Using Round (coverting VB6 code to SQL) 5 42
    VBA to format 3 64 2008 2 39
    vb6 connector to SQL Server 2 20
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now