Results 1 to 9 of 9

Thread: [RESOLVED] VB & Excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    76

    Resolved [RESOLVED] VB & Excel

    I would like to know ... How can we put data into an Excel spreadsheet from a form.

    If I have Name & Proffession fields in my form and I want these to be stored in different cells of an Excel sheets...How can I do it .. ?

  2. #2
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: VB & Excel

    you can do that using ado and sql statement (insert statement)
    refer to this thread also
    http://vbforums.com/showthread.php?t=394651
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

  3. #3
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: VB & Excel

    First you have to reference the Excel library in Projects/Reference Menu in VB6.
    Then you create a ExcelApp Object and a Worksheet object.

    The Worksheet Object has a Cells or a Range object that you can manipulate.

    Try the following code:

    Dim objExcel as Excel.Application
    Set objExcel = New Excel.Application
    objExcel.Visible = True
    objExcel.SheetsInNewWorkbook = 1
    objExcel.Workbooks.Add

    With objExcel.ActiveSheet
    .Range("A1").value = "Heading 1"
    .Range("B1").value = "Data 1"
    etc, etc,etc....
    End With
    Should do it.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    76

    Re: VB & Excel

    Silvy ... I am sorry ... can you give me sample code...

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: VB & Excel

    You still need to add a reference to the Excel library in projects-> references menu
    VB Code:
    1. ' Create our new objects
    2. ' Global Declarations
    3. Dim objExcel as Excel.Application
    4. dim RowCount as integer
    5. sub Form_Load()
    6. Set objExcel = New Excel.Application
    7. objExcel.Visible = True
    8. objExcel.SheetsInNewWorkbook = 1
    9. objExcel.Workbooks.Add
    10.  
    11. 'This refers to our active sheet
    12.  
    13. With objExcel.ActiveSheet
    14. .Range("A1").value = "Name"
    15. .Range("B1").value = "Profession"
    16. RowCount = 1
    17. End With
    18.  
    19. end sub
    20. sub SaveText(byval strName as string, byval strProf as string)
    21. RowCount = RowCount + 1
    22. with objexcel.activesheet
    23. .range("A" & rowcount).value = strName
    24. .range("B" & RowCount).value = strProf
    25. end with
    26. end sub
    27.  
    28. 'To add stuff, say from a commandbutton:
    29. Sub Command1_Click()
    30. SaveText(txtName.text, txtProf.text)
    31. txtName.text = ""
    32. txtProf.text = ""
    33. end sub

    Does that help?

  6. #6
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: VB & Excel

    one good way is to export all the contents of a flex grid to an excel sheet.
    refer si_the_geek's manual on excel .u will find all that u need .also rhinobull has some excellent code on this topic!!

    good luck
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: VB & Excel

    If you're still using the code i posted, i found it was always handy to record a macro in VBA (i.e. in excel), doing what i wanted to do, then recoding that macro in vb

    just a suggestion

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

    Re: VB & Excel

    I fully agree with that - part of my tutorial explains how to convert the macro to code in your VB program.
    Quote Originally Posted by litlewiki
    refer si_the_geek's manual on excel
    Link below

  9. #9
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Re: VB & Excel

    If you have Excel installed check out my Tutorial:

    http://www.vbforums.com/showthread.p...ighlight=Excel

    If you don't have Excel installed check out this ADO method:

    http://www.vbforums.com/showthread.p...ighlight=Excel
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

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