Click here to Skip to main content
15,888,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Guys need your help I get this Error "Index was out of range. Must be non negative and less than the size of the collection. Parameter name; Index." Please help me Im a newbie T____T

Here's the code of mine :

VB
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try
            Cursor = Cursors.WaitCursor
            Dim objExcel As New Excel.Application
            Dim bkWorkBook As Workbook
            Dim shWorkSheet As Worksheet
            Dim i As Integer
            Dim j As Integer
            objExcel = New Excel.Application
            bkWorkBook = objExcel.Workbooks.Add
            shWorkSheet = CType(bkWorkBook.ActiveSheet, Worksheet)

            shWorkSheet.Cells(1, 1) = "ACTIVITY LOGS"
            shWorkSheet.Cells(1, 1).EntireRow.Font.Bold = True
            shWorkSheet.Cells(1, 1).EntireRow.Font.size += 5
            shWorkSheet.Range("A1", "E1").MergeCells = True

            For i = 0 To Me.ListView1.Columns.Count - 1
                shWorkSheet.Cells(3, i + 1) = UCase(Me.ListView1.Columns(i).Text)
                shWorkSheet.Cells(3, i + 1).EntireRow.Font.Bold = True
            Next
            For i = 0 To Me.ListView1.Items.Count - 1
                For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1
                    shWorkSheet.Cells((i + 2) + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
                Next
            Next

            shWorkSheet.Cells((i + 2) + 4, 1) = UCase("Prepared by: " & MainMDi.StatusStrip.Items(2).Text)
            shWorkSheet.Cells((i + 2) + 4, 1).EntireRow.Font.Bold = True

            objExcel.Visible = True
            'EventLog("Export to Excel", "Activity Logs", "Extraction of data from: " & DTP1.Value & " to " & DTP2.Value & ".")
            'For Each p As Process In Process.GetProcesses
            '    If p.ProcessName = "Excel" Then
            '        p.Kill()
            '    End If
            'Next
            Cursor = Cursors.Default
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
End Sub 
Posted
Updated 25-Sep-13 21:56pm
v2
Comments
ArunRajendra 26-Sep-13 3:53am    
Which line are you getting this error?
ChiPats 26-Sep-13 3:57am    
At the button Click T_T
ArunRajendra 26-Sep-13 3:59am    
Yes I know this is the click event. In which within the event your getting the error.
Thanks7872 26-Sep-13 4:01am    
:laugh:

To progress towards a more skilled kind of developer, the newbie should learn how to read (and report) error messages (and use the debugger). In your code there are several indexed objects, each of them could have thrown such an exception. Please read carefully the error message, and, if you are really unable to cope with it, report its exact content here.
 
Share this answer
 
Try removing the -1's
if the count is zero then you subtract 1 then it will be -1, a negitive number.
 
Share this answer
 
You should test your vars before iterating, ex :

VB
If Me.ListView1.Columns.Count>0 Then
            For i = 0 To Me.ListView1.Columns.Count - 1
                shWorkSheet.Cells(3, i + 1) = UCase(Me.ListView1.Columns(i).Text)
                shWorkSheet.Cells(3, i + 1).EntireRow.Font.Bold = True
            Next
Else
' do something
End If
 
Share this answer
 

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