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

    Problem adding additional rows of data

    I'm am very new to Vb.net, I have coded in VBA.

    I have created a form that will open Microsoft Excel, add header text to the columns, and insert the text from a few of the controls located on my form ("not finished adding controls") when I click the Submit button.

    My problem is that when I enter different data into those controls and click the submit button again, I get a new Excel Workbook with the new data.

    I need the new data the be submitted to the .Activecell.Offset(1,0) of the already open workbook.

    Ultimate Goal of Submit button is to open new Excel workbook, Enter multiple rows of data via Visual Basic Form.

    Then have a "btnSave" to; Save and Close the Excel workbook.

    I also need to transfer an image from the form to the workbook so that the stays linked to the that particular row of data.

    Here is my present code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click

    'Start a new workbook in Excel.
    oExcel = CreateObject("Excel.Application")
    oBook = oExcel.Workbooks.Add

    'Add data to cells of the first worksheet in the new workbook.
    oSheet = oBook.Worksheets(1)

    With oExcel
    .visible = True
    '.Workbooks.Open("C:\Documents and Settings\Credit Desk\My Documents\Visual Studio 2008\Projects\New Item Form\New Items Excel Data.xls")

    .ActiveCell.Value = "MFG#"
    .ActiveCell.Offset(0, 1).Value = "FINE LINE"
    .ActiveCell.Offset(0, 2).Value = "BUYER"
    .ActiveCell.Offset(0, 3).Value = "NON-HAZARD"
    .ActiveCell.Offset(0, 4).Value = "UN NUMBER"
    .ActiveCell.Offset(0, 5).Value = "ORM-D NUMBER"
    .ActiveCell.Offset(0, 6).Value = "VENDOR #"
    .ActiveCell.Offset(0, 7).Value = "VENDOR NAME"
    .ActiveCell.Offset(0, 8).Value = "SUB-LIST #"
    .ActiveCell.Offset(0, 9).Value = "DESCRIPTION"
    .ActiveCell.Offset(0, 10).Value = "SHIP UNIT"
    .ActiveCell.Offset(0, 11).Value = "STOCK PACK"
    .ActiveCell.Offset(0, 12).Value = "N-BREAK"
    .ActiveCell.Offset(0, 13).Value = "CARTON QTY"
    .ActiveCell.Offset(0, 14).Value = "ITEM WEIGHT"
    .ActiveCell.Offset(0, 15).Value = "CASH DISC."
    .ActiveCell.Offset(0, 16).Value = "STK HHH"
    .ActiveCell.Offset(0, 17).Value = "STK PER"
    .ActiveCell.Offset(0, 18).Value = "BUY MULTI"
    .ActiveCell.Offset(0, 19).Value = "INNER CRTN UPC"
    .ActiveCell.Offset(0, 20).Value = "CNTRY CODE"
    .ActiveCell.Offset(0, 21).Value = "TARGET GM"
    .ActiveCell.Offset(0, 22).Value = "BASE COST"
    .ActiveCell.Offset(0, 23).Value = "NET SELL PRC"
    .ActiveCell.Offset(0, 24).Value = "RETAIL SENS."
    .ActiveCell.Offset(0, 25).Value = "RETAIL PK"
    .ActiveCell.Offset(0, 26).Value = "STKR FACTOR"
    .ActiveCell.Offset(0, 27).Value = "RETAIL GRP %"
    .ActiveCell.Offset(0, 28).Value = "RETAIL CLASS"
    .ActiveCell.Offset(0, 29).Value = "RETAIL PACKAGE"
    .ActiveCell.Offset(0, 30).Value = "WHSE MAX SHIP"
    .ActiveCell.Offset(0, 31).Value = "INNER CRTN DIM L"
    .ActiveCell.Offset(0, 32).Value = "INNER CRTN DIM W"
    .ActiveCell.Offset(0, 33).Value = "INNER CRTN DIM H"
    .ActiveCell.Offset(0, 34).Value = "OUTER CRTN UPC"
    .ActiveCell.Offset(0, 35).Value = "OUTER CRTN DIM L"
    .ActiveCell.Offset(0, 36).Value = "OUTER CRTN DIM W"
    .ActiveCell.Offset(0, 37).Value = "OUTER CRTN DIM H"
    .ActiveCell.Offset(0, 38).Value = "EXT DESCRIPTION"
    .ActiveCell.Offset(0, 39).Value = "PICTURE"

    .Range("A2").select()
    Dim Description As String = Me.TxtDesc.Text
    .ActiveCell.Value = Microsoft.VisualBasic.Left(Description, (InStr(Description, " ") - 1))
    .ActiveCell.Offset(0, 1).Value = Me.ComboBox3.Text
    .ActiveCell.Offset(0, 2).Value = Me.TxtDesc.Text
    Dim NonHaz As String
    If Me.TxtORMD.Text = "" And Me.TxtUnNo.Text = "" Then
    NonHaz = "X"
    .ActiveCell.Offset(0, 3).Value = NonHaz
    Else : .ActiveCell.Offset(0, 3).Value = ""

    End If

    .Columns("A:AN").EntireColumn.AutoFit()
    .ActiveCell.Offset(1, 0).select()

    End With


    End Sub

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Problem adding additional rows of data

    You have to open a worksheet when the program starts, not when you click the button.

    When you click the button, check to see if it's open, and has written a line.

    Change your logic around a little. You can add a line when you click the button, or add them to a list, until you click DONE
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2009
    Posts
    2

    Re: Problem adding additional rows of data

    That works great, Thank you so much.

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