Results 1 to 5 of 5

Thread: updating data in excel file

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    3

    updating data in excel file

    hey guys, im fairly new to vb, and so would like a bit of help with this.
    I have a vb form with textbox inputs and am trying to store the inputs to an existing excel file. Basically i need to open the excel file, write the data to a row, and save it. Then each time the program is run it should add the new values to the next row down in the file. any help with this would be much appreciated.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: updating data in excel file

    Moved to Office Development forum

    You can do this fairly easily using automation code. To do this, add a reference ("Project"->"References") to "Microsoft Excel X.X Object library", then you can use code like this:
    VB Code:
    1. Dim oXLApp as Excel.Application         'Declare the object variables
    2. Dim oXLBook as Excel.Workbook
    3. Dim oXLSheet as Excel.Worksheet
    4.  
    5.   Set oXLApp = New Excel.Application    'Create a new instance of Excel
    6.   Set oXLBook = oXLApp.Workbooks.Open("[u]C:\folder\file.xls[/u]")    'Open your workbook
    7.   Set oXLSheet = oXLBook.Worksheets(1)  'Work with the first worksheet
    8.  
    9. Dim lngRow as Long
    10.   With oXLSheet           'Find the row to write to
    11.     lngRow = .UsedRange.Rows.Count + 1
    12.                           'Write the values
    13.     .Cells(lngRow, [U]1[/U]) = [U]Text1.Text[/U]
    14.     .Cells(lngRow, [U]2[/U]) = [U]Text2.Text[/U]
    15.   End With
    16.  
    17.   Set oXLSheet = Nothing             'disconnect from the Worksheet
    18.   oXLBook.Close SaveChanges:= True   'Save (and disconnect from) the Workbook
    19.   Set oXLBook = Nothing
    20.   oXLApp.Quit                        'Close (and disconnect from) Excel
    21.   Set oXLApp = Nothing
    Last edited by si_the_geek; Feb 10th, 2006 at 07:10 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    3

    Re: updating data in excel file

    Nice one mate thanks a lot, the only query I have is that I get an error message on this line

    lngRow = UsedRange.Rows.Count + 1

    stating "object required"

    Any ideas on this?

    Cheers

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    3

    Re: updating data in excel file

    Edit reply. Problem solved all sorted. Thanks a lot mate, good stuff.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: updating data in excel file

    Ah yes, I missed the dot! (now corrected in my post above)

    As you have the answer, could you do us a favour and mark this thread as resolved? Just above the first post is a 'Thread tools' menu which contains 'Mark thread resolved'.

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