Results 1 to 12 of 12

Thread: Text box info from vb to Excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    manchester, england
    Posts
    84

    Text box info from vb to Excel

    Ok this is my problem, I have a program that calculates costs and displays the costs in 3 text boxes. I need to be able for VB to open a new excel sheet and put the info from the text boxes in to the cells.

    This is the code I have:

    Dim x1app As Excel.Application
    Dim x1book As Excel.Workbook
    Dim x1sheet As Excel.Worksheet

    Set x1app = New Excel.Application
    Set x1book = x1app.Workbooks.Add
    Set x1sheet = x1book.Worksheets.Add

    x1sheet.Cells(1, 1).Value = Text1.Text
    x1sheet.Cells(2, 1).Value = Text2.Text

    x1sheet.Cells(3, 1).Formula = "r1c1 +r2c1"
    Text3.Text = x1sheet.Cells(3, 1)


    But when I run it I get an error saying: Invalid use of new key word and the line
    Set x1app = New Excel.Application is highlighted.

    Please can some one help

  2. #2
    Lively Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    71

    Excel

    Try this:
    Set x1app = CreateObject("Excel.application")

    in place of this:
    Set x1app = New Excel.Application

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    manchester, england
    Posts
    84
    it worked!!!!!

    Thanks mate I really appreciate it.

  4. #4
    Hyperactive Member tomjess's Avatar
    Join Date
    Mar 2001
    Location
    Hamilton, New Zealand
    Posts
    348

    Any ideas

    I have used the code in this thread and it seems to work, the only problem I have is that I can't find the excel sheet, called Book1, it has opened. If I try again it opens another excel sheet, Book2, which also can't find.
    It is only when I shut down my computer, it asks me do I want to save the changes in Book1 and Book2.
    Any ideas to where they are and how I can find them.
    kia kaha kia maia - give of your best, be confident in your own ability

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Couldn't test it sorry, (Still yet to install Office after a clean format), but this should solve your problems:
    VB Code:
    1. Dim x1app As Excel.Application
    2. Dim x1book As Excel.Workbook
    3. Dim x1sheet As Excel.Worksheet
    4.  
    5. Set x1app = CreateObject("Excel.application")
    6. Set x1book = x1app.Workbooks.Add
    7. Set x1sheet = x1book.Worksheets.Add
    8.  
    9. x1app.Visible = True
    10.  
    11. x1sheet.Cells(1, 1).Value = Text1.Text
    12. x1sheet.Cells(2, 1).Value = Text2.Text
    13.  
    14. x1sheet.Cells(3, 1).Formula = "r1c1 +r2c1"
    15. Text3.Text = x1sheet.Cells(3, 1)

  6. #6
    Hyperactive Member tomjess's Avatar
    Join Date
    Mar 2001
    Location
    Hamilton, New Zealand
    Posts
    348

    thank you

    da_silvy

    Thank you

    steve
    kia kaha kia maia - give of your best, be confident in your own ability

  7. #7
    Hyperactive Member tomjess's Avatar
    Join Date
    Mar 2001
    Location
    Hamilton, New Zealand
    Posts
    348

    How do I

    Thanks for the help above.

    How do I go about calling a particular .xls file, ie say I have a Test.xls and I want to bring that up.

    What coding would need to be put in the above coding for that to work

    Thanks guys
    kia kaha kia maia - give of your best, be confident in your own ability

  8. #8
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    VB Code:
    1. Dim objExcel As Excel.Application
    2.    
    3.     On Error Resume Next
    4.     Set objExcel = GetObject(",Excel.Application")
    5.     If Err.Number <> 0 Then
    6.         Set objExcel = CreateObject("Excel.Application")
    7.     End If
    8.     Err.Clear
    9.     On Error GoTo 0
    10.    
    11.     objExcel.Visible = True
    12.     objExcel.WindowState = xlNormal
    13.     objExcel.Workbooks.Open FileName:="C:\Book5.xls"

  9. #9
    Hyperactive Member tomjess's Avatar
    Join Date
    Mar 2001
    Location
    Hamilton, New Zealand
    Posts
    348

    Text for Textbox, what for Combobox?

    Hi Guys and Gals

    I know that I can use .text for a text box to take through to Excel. What can I use to take the text thats in a Combobox through to Excel?

    Below is what I am using

    objExcel.Cells(23, 9).Value = txtLast10.Text

    objExcel.Cells(5, 10).Value = cboDept1.????

    Thanks once again
    kia kaha kia maia - give of your best, be confident in your own ability

  10. #10
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    objExcel.Cells(5, 10).Value = CboDept1.List(0) will give you the 1st item in the combobox.(the list in combos start at zero)

    If you want the 10th item, objExcel.Cells(5, 10).Value = CboDept1.List(9)

  11. #11
    Hyperactive Member tomjess's Avatar
    Join Date
    Mar 2001
    Location
    Hamilton, New Zealand
    Posts
    348

    Thanks, but what if

    Thanks for that coding

    But what if the user has decided to use say one the items in the combox. Say he also has 10 items in the combox. I will not know which one he has selected, so I want to have the one he has selected to go through to Excel. Does that make sense? I hope so.

    Thanks
    kia kaha kia maia - give of your best, be confident in your own ability

  12. #12
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    objExcel.Cells(5, 10).Value = CboDept1.List(CboDept1.ListIndex)

    .ListIndex gets the index of the currently selected file

    Cheers

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