Click here to Skip to main content
13,767,285 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
        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 13-Sep-18 21:30pm
Updated 13-Sep-18 23:28pm
v3

1 solution

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

Solution 1

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

See: Range.Delete(Object) Method (Microsoft.Office.Interop.Excel) | Microsoft Docs[^]
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.181114.1 | Last Updated 14 Sep 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100