Consulting

Results 1 to 4 of 4

Thread: Solved: Writ data to Excel problem

  1. #1

    Question Solved: Writ data to Excel problem

    Hi I'm using vb.net
    I tried this code to write data to Excel File, already open.but didn't success with me.may you help
    [VBA]Dim exl As New Excel.Application Dim exlWorkSheet As Excel.Worksheet exlWorkSheet = exl.Workbooks("ta1").Worksheets("Sheet") exlWorkSheet.Cells(2, 6).Value = "hello"[/VBA]

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Just guessing, but in VBA you need the 'Set' to reference objects


    [VBA
    Dim exl As New Excel.Application
    Dim exlWorkSheet As Excel.Worksheet
    Set exlWorkSheet = exl.Workbooks("ta1").Worksheets("Sheet")
    exlWorkSheet.Cells(2, 6).Value = "hello"
    [/VBA]

    Paul

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    Quote Originally Posted by Nader
    Hi I'm using vb.net...
    As Paul mentioned, we are using VBA, not VB.Net.

    Quote Originally Posted by Nader
    I tried this code to write data to Excel File, already open.but didn't success with me
    Okay, presumably you mean that the file is already opened in the same instance of Excel that the code is running in. You appear to be trying to reference the workbook in a new, auto-instanced, instance of Excel. I believe you'd need to open the file in the new instance (disregarding for the moment, reopening a file that is already opened).

    With the file closed, I suppose you could do something like:
    [vba]
    Sub ex()
    Dim exl As New Excel.Application
    Dim exlWorkSheet As Excel.Worksheet
    Dim wb As Workbook

    Set wb = exl.Workbooks.Open(ThisWorkbook.Path & "\wb.xlsx")
    exl.Visible = True
    Set exlWorkSheet = exl.Workbooks("wb.xlsx").Worksheets("Sheet2")
    exlWorkSheet.Cells(2, 6).Value = "hello"
    End Sub
    [/vba]

    I would not auto-instance though. You may find this interesting:
    http://www.cpearson.com/excel/classes.aspx

    Hope thta helps,

    Mark

  4. #4
    Thank you for help!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •