Click to See Complete Forum and Search --> : Problem adding additional rows of data


cman_c
July 2nd, 2009, 01:16 PM
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

dglienna
July 2nd, 2009, 06:11 PM
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

cman_c
July 2nd, 2009, 10:38 PM
That works great, Thank you so much.