I'm trying to find a quicker way to paste the values of the DataGridView cells to an Excel spreadsheet. The code below works but it's really slow as it loops through each cell. Hoping someone please help with speeding this up.
Thanks in advance.
For i = 0 To PartsDataGridView.RowCount - 1
If PartsDataGridView.Rows(i).Cells(0).Value = "Positive" Then
lr3 = WS.Range("A" & WS.Rows.Count).End(Excel.XlDirection.xlUp).Row + 1
With WS
.Range("A" & lr3).Value = PartsDataGridView.Rows(i).Cells(1).Value
.Range("B" & lr3).Value = PartsDataGridView.Rows(i).Cells(2).Value
.Range("C" & lr3).Value = PartsDataGridView.Rows(i).Cells(3).Value
.Range("D" & lr3).Value = PartsDataGridView.Rows(i).Cells(4).Value
.Range("E" & lr3).Value = PartsDataGridView.Rows(i).Cells(5).Value
.Range("F" & lr3).Value = PartsDataGridView.Rows(i).Cells(6).Value
.Range("G" & lr3).Value = PartsDataGridView.Rows(i).Cells(7).Value
.Range("H" & lr3).Value = PartsDataGridView.Rows(i).Cells(8).Value
.Range("I" & lr3).Value = PartsDataGridView.Rows(i).Cells(9).Value
.Range("J" & lr3).Value = PartsDataGridView.Rows(i).Cells(10).Value
.Range("K" & lr3).Value = PartsDataGridView.Rows(i).Cells(11).Value
.Range("L" & lr3).Value = PartsDataGridView.Rows(i).Cells(12).Value
End With
End If
Next
If I am not mistaken you can paste arrays into Excel Interop Ranges.
I used something like this:
In this case array is two dimensional array of your data
Worksheet.Range("A2:T" + array.GetLength(0).ToString).Value2 = array
This increased my speed but I was working with thousands of Rows, I am not sure by how much this will increase your code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.