dcsimg
Results 1 to 2 of 2

Thread: EXCEL- Delete rows and add column

  1. #1

    Thread Starter
    Registered User
    Join Date
    May 2019
    Posts
    1

    EXCEL- Delete rows and add column

    Hi. I am beginner for programming. I really noob for programming coding, so hope anyone would help me.
    I want to delete some rows (12 rows) in excel, using interop.excel.
    also add few columns.

    This is fully my code,

    Code:
      Try
                Dim excelApplication = New Application
                excelApplication.Visible = True
                Dim excelWorkbook As Workbook
                excelWorkbook = excelApplication.Workbooks.Add()
                excelWorkbook.SaveAs(targetPath)
                Dim excelWorksheet As Worksheet = excelWorkbook.Worksheets.Add()
                excelWorksheet.Name = "Import"
            
                Return excelWorksheet
                'excelWorksheet.Cells(0, 11).EntireRow.Delete()
                'Delete 5 rows now. (8th row - 12th row)
                'excelWorksheet.Cells.DeleteRows(0, 5)
    
                ' Dim rg As Excel.Range = excelWorksheet.Rows("1:5") ' delete the specific row
                'Dim rg As Excel.Range = oSheet.Columns("A:A") ' delete the specific column
                ' rg.Select()
                ' rg.Delete()
                ' System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet)
           
    
     Catch ex As Exception
                Debug.WriteLine("The excel worksheet could not be created:")
                Debug.WriteLine(ex.Message)
            End Try
            Return Nothing
    this is the code I try to delete 12 rows,
    Code:
         'excelWorksheet.Cells(0, 11).EntireRow.Delete()
                'Delete 5 rows now. (8th row - 12th row)
                'excelWorksheet.Cells.DeleteRows(0, 5)
    
                ' Dim rg As Excel.Range = excelWorksheet.Rows("1:5") ' delete the specific row
                'Dim rg As Excel.Range = oSheet.Columns("A:A") ' delete the specific column
                ' rg.Select()
                ' rg.Delete()
                ' System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorksheet)
    But unfortunately, it doesnt delete the rows.
    Please someone guide me. Thank you very much everyone.

  2. #2
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,863

    Re: EXCEL- Delete rows and add column

    Hi,

    you just about had it right

    try this
    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Try
                Dim xlApp As New Microsoft.Office.Interop.Excel.Application()
                Dim xlWb As Microsoft.Office.Interop.Excel.Workbook '= xlApp.Workbooks.Add
                xlWb = xlApp.Workbooks.Open("E:\vbExcel.xlsx")
                Dim xlSt As Microsoft.Office.Interop.Excel.Worksheet = xlWb.ActiveSheet
                'rows to delete
                xlSt.Range("A13:A15").EntireRow.Delete()
                xlWb.Save()
                xlApp.Quit()
                xlApp = Nothing
            Catch g As Exception
                MsgBox(g.ToString)
            End Try
        End Sub
    you can't just say ...1:5
    Code:
      ' Dim rg As Excel.Range = excelWorksheet.Rows("1:5") ' delete the specific row
    always when using Range it hould look like this =
    Code:
    Range("A13:A15")
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width