CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2001
    Posts
    67

    Updating an excel document

    I have this programme which adds information to an Excel document in a few columns and rows. I would like to have the ability to add in
    new information to the same document after I have exit and then reentered the programme a new time. How can I know which row to continue adding
    my new information after I have exit?

    Is there a easy way to do this ?


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Updating an excel document

    THis code will help upo to find last/first filled col/row

    Dim excel_app As Object
    Dim excel_sheet As Object
    Dim new_value As String
    Dim first_row As Integer
    Dim first_col As Integer
    Dim num_rows As Integer
    Dim num_cols As Integer

    ' Create the Excel application.
    Set excel_app = CreateObject("Excel.Application")

    ' Uncomment this line to make Excel visible.
    ' excel_app.Visible = True

    ' Open the Excel spreadsheet.
    excel_app.Workbooks.Open FileName:=txtExcelFile.Text

    ' Check for later versions.
    If Val(excel_app.Application.Version) >= 8 Then
    Set excel_sheet = excel_app.ActiveSheet
    Else
    Set excel_sheet = excel_app
    End If

    ' Get and display the bounds.
    first_row = excel_sheet.UsedRange.Row
    first_col = excel_sheet.UsedRange.Column
    num_rows = excel_sheet.UsedRange.Rows.Count
    num_cols = excel_sheet.UsedRange.Columns.Count

    MsgBox "Rows: " & Format$(first_row) & _
    " - " & Format$(first_row + num_rows - 1) & vbCrLf & _
    "Cols: " & Format$(first_col) & _
    " - " & Format$(first_col + num_cols - 1)

    ' Comment the rest of the lines to keep
    ' Excel running so you can see it.

    ' Close the workbook without saving.
    excel_app.ActiveWorkbook.Close False

    ' Close Excel.
    excel_app.Quit
    Set excel_sheet = Nothing
    Set excel_app = Nothing

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

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

Featured