Click here to Skip to main content
15,886,666 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Guys Help me to fix this code thanks in advance
Its make me headache to find out but cant to find to easy way
I want to delete the row where can i select the cell in datagridview to delete in Excel 2010 but always show the error. thanks again.

What I have tried:

VB
    Dim ExcelApp As Excel.Application
    Dim ExcelWorkBk As Excel.Workbook
    Dim ExcelWorkSht As Excel.Worksheet
Public Sub ExcelRemoveRow(ByVal FileName As String, ByVal SheetName As String, ByVal RowIndex As Int32)
        ExcelApp = New Excel.Application
        Try
            'This code are another form and this is my sample code
            'Filename = GetAppPath() & "\backup\cbms.xlsx"
            'ExcelRemoveRow(Filename, "enumeratorrecord", DataGridView1.CurrentRow.Cells(0).Value)

            ExcelWorkBk = ExcelApp.Workbooks.Open(FileName) 'error code here

            '--------------------------ERROR-------------------------------------
            'Excel cannot open the file 'cbms.xlsx' because the file format 
            'or file extension is not valid. Verify that the file has not been 
            'corrupted and that the file extension matches the format of the file.
            ExcelWorkSht = ExcelWorkBk.Worksheets("enumeratorrecord")

            'Dim empty As Integer
            'For empty = i + 1 To 2000
            ExcelWorkSht.Rows(RowIndex).Delete()
            'Next
            ExcelWorkBk.Save()
            grideview()
            MessageBox.Show("Record Deleted Successfully", "Process Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            If Not IsNothing(ExcelWorkBk) Then
                ExcelWorkBk.Close()
            End If
            ExcelApp.Quit()
        End Try
    End Sub
Posted
Updated 13-Sep-18 22:28pm
v3

1 solution

You forgot to pass Shift argument in this line:
VB
ExcelWorkSht.Rows(RowIndex).Delete(_Here_)

See: Range.Delete(Object) Method (Microsoft.Office.Interop.Excel) | Microsoft Docs[^]
 
Share this answer
 
Comments
CodieCalm 14-Sep-18 4:32am    
its C# not vb.net
Maciej Los 14-Sep-18 4:37am    
Well...
Go to the top of page. Look at the right-top corner. There's a small switch between languages (C#, VB).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900