offset function in vb forms

imti321 0 Tallied Votes 381 Views Share

'the code is working properly only problem with this code is when the first line ends in excel and data is inserted in next Row "instead of inserting another row it copies the same content which is inseted last time in form "

Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click
        Dim MyExcel As Microsoft.Office.Interop.Excel.Application
        MyExcel = New Microsoft.Office.Interop.Excel.Application
        Dim wb As Microsoft.Office.Interop.Excel.Workbook
        wb = MyExcel.Workbooks.Open("C:\Users\IMTIYAZ\Desktop\test")
        Dim ws As Microsoft.Office.Interop.Excel.Worksheet
        ws = wb.Sheets("gp")
        ws.Cells(3, 1).value = txtSn.Text
        ws.Cells(3, 2).Value = txtNa.Text
        ws.Cells(3, 3).Value = txtGpf.Text
        ws.Cells(3, 4).Value = txtBa.Text
        ws.Cells(3, 5).Value = txtBn.Text
        ws.Cells(3, 6).Value = txtAp.Text
        ws.Cells(3, 7).Value = txtBp.Text
        ws.Cells(3, 8).Value = txtGp.Text
        ws.Range("I3").Formula = ("=G3+H3")
        Lbl_Tt.Text = ws.Cells(3, 9).Value
        ws.Cells(3, 10).Value = txtPp.Text
        ws.Cells(3, 11).Value = txtDa.Text
        ws.Cells(3, 12).Value = txtMa.Text
        ws.Cells(3, 13).Value = txtRa.Text
        ws.Cells(3, 14).Value = txtCa.Text
        ws.Cells(3, 15).Value = txtOa.Text
        ws.Range("P3").Formula = ("=i3+J3+K3+L3+M3+N3+O3")
        Lbl_Gt.Text = ws.Cells(3, 16).Value
        ws.Cells(3, 17).Value = txtFa.Text
        ws.Range("r3").Formula = ("=P3-Q3")
        Lbl_Naf.Text = ws.Cells(3, 18).Value
        ws.Cells(3, 19).Value = txtSf.Text
        ws.Cells(3, 20).Value = txtRf.Text
        ws.Cells(3, 21).Value = txtSi1.Text
        ws.Cells(3, 22).Value = txtSi2.Text
        ws.Cells(3, 23).Value = txtSi3.Text
        ws.Range("x3").Formula = ("=S3+T3+V3+W3+U3")
        Lbl_Td.Text = ws.Cells(3, 24).Value
        ws.Range("y3").Formula = ("=R3-X3")
        Lbl_Nad.Text = ws.Cells(3, 25).Value
        ws.Cells(3, 26).Value = txtHl.Text
        ws.Cells(3, 27).Value = txtCsc.Text
        ws.Cells(3, 28).Value = txtMr.Text
        ws.Cells(3, 29).Value = txtIt.Text
        ws.Range("ad3").Formula = ("=Y3-(AC3+Z3+AA3+AB3)")
        Lbl_Np.Text = ws.Cells(3, 30).Value
        Me.Update()
        MyExcel.ActiveWorkbook.SaveCopyAs("test2")
        Dim RowCount As Long
        With wb.ActiveSheet.Range("f3")
            .offset(RowCount + 1, -5).activate()
            .offset(RowCount + 1, -5).value = Me.txtSn.Text

        End With
        MyExcel.Quit()
        MyExcel = Nothing
    End Sub
imti321 35 Light Poster

No that post is different than this one that is related to connecting to database.I have created a form in vb and then used excel to output my data .i mean i used excel at backend.
My problem is first row gets fine but when i try to put another row through form it overwrites the already written first row.

tinstaafl 1,176 Posting Maven

It looks like you're declaring RowCount every time the routine is run, which will reset it's value. Try making it global.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.