# Visual Basic > Visual Basic .NET > VS 2022 [RESOLVED] Delete records in Excel

## sacramento

hi to all,

I have write this little code to delete some records in an excel file, but the code give a n error in the line mark as bold



```
Dim xlApp As excel.Application = New Microsoft.Office.Interop.Excel.Application()

        excelLocation = My.Application.Info.DirectoryPath & "\Documentos\Formulario_Averbamento.xlsx"

        xlApp.DisplayAlerts = False
        Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(excelLocation, 0, False, 5, "", "",
         False, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", True, False, 0,
         True, False, False)
        Dim worksheets As excel.Sheets = xlWorkBook.Worksheets


        var_contar_sheets = var_contar_sheets + 25


        For i = 26 To var_contar_sheets
            worksheets.Delete()
        Next

        xlWorkBook.Save()
        xlWorkBook.Close()
```


Any help please
Thanks

----------


## ChrisE

your are deleteing the whole worksheet


```
 worksheets.Delete()
```


to delete some rows or range it should look like this


```
'....
 Dim xlSt As Microsoft.Office.Interop.Excel.Worksheet = xlWb.ActiveSheet
            'rows to delete
            'xlSt.Range("A13:A19").EntireRow.Delete()
'....
```

----------


## sacramento

HI,

thanks for your reply.

What is xlwb?

----------


## sacramento

HI,

Someone Knows why when i add records to excel file the process open i lot of process's in System like in the picture?
Attachment 186495

----------


## ChrisE

> HI,
> 
> thanks for your reply.
> 
> What is xlwb?


that is the Workbook, here a sample


```
 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            Dim xlApp As New Microsoft.Office.Interop.Excel.Application()
            Dim xlWb As Microsoft.Office.Interop.Excel.Workbook
            xlWb = xlApp.Workbooks.Open("E:\vbExcel.xlsx")
            Dim xlSt As Excel.Worksheet = CType(xlWb.Worksheets("Sheet3"), Excel.Worksheet)
            'rows to delete
            xlSt.Range("A18:A20").EntireRow.Delete()
            xlWb.Save()
            xlApp.Quit()
            xlApp = Nothing
        Catch g As Exception
            MsgBox(g.ToString)
        End Try
    End Sub
```

----------


## sacramento

Thanks....Work Perfectlly

----------

