Click here to Skip to main content
14,634,521 members
Rate this:
Please Sign up or sign in to 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:

    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
            '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

            '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
            MessageBox.Show("Record Deleted Successfully", "Process Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            If Not IsNothing(ExcelWorkBk) Then
            End If
        End Try
    End Sub
Updated 13-Sep-18 22:28pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You forgot to pass Shift argument in this line:

See: Range.Delete(Object) Method (Microsoft.Office.Interop.Excel) | Microsoft Docs[^]
CodieCalm 14-Sep-18 4:32am
its C# not
Maciej Los 14-Sep-18 4:37am
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100