Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

With this i copy a datagridview to a cetain row in a excel sheet.

VB
Private Sub copyAlltoClipboard()
      DataGridView1.SelectAll()
      Dim dataObj As DataObject = DataGridView1.GetClipboardContent()
      If dataObj IsNot Nothing Then Clipboard.SetDataObject(dataObj)
  End Sub

  Private Sub Crew2()
      copyAlltoClipboard()
      Dim xlexcel As Excel.Application
      Dim xlWorkBook As Excel.Workbook
      Dim xlWorkSheet As Excel.Worksheet
      xlexcel = New Excel.Application
      xlexcel.Visible = True
      xlWorkBook = xlexcel.Workbooks.Open("C:\weg\Jaja.xlsx")
      xlWorkSheet = xlWorkBook.Worksheets(1)

      Dim CR As Excel.Range = xlWorkSheet.Cells(12, 3)
      CR.PasteSpecial()

  End Sub


But i what to paste Datagridview2 also in this excel sheet.

How do i find the next empty row or can i paste 2 datagridviews in 1 go ?

What I have tried:

Don't know how to fix it

Or maybe combine 2 datagridview in one ? before paste
Posted
Updated 5-Feb-19 4:11am
Comments
Maciej Los 5-Feb-19 11:09am    
Does DataGridViews are binded with data or not?
BASSIES 5-Feb-19 14:58pm    
This is working , find last row but it is slow

Private Sub copyAlltoClipboard()
DataGridView1.SelectAll()
Dim dataObj As DataObject = DataGridView1.GetClipboardContent()
If dataObj IsNot Nothing Then Clipboard.SetDataObject(dataObj)
End Sub
Private Sub copyAlltoClipboard2()
DataGridView2.SelectAll()
Dim dataObj As DataObject = DataGridView2.GetClipboardContent()
If dataObj IsNot Nothing Then Clipboard.SetDataObject(dataObj)
End Sub

Private Sub Crew2()
copyAlltoClipboard()
Dim xlexcel As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlexcel = New Excel.Application
xlexcel.Visible = True
xlWorkBook = xlexcel.Workbooks.Open("C:\weg\Verbruik.xlsx")
xlWorkSheet = xlWorkBook.Worksheets(1)

Dim CR As Excel.Range = xlWorkSheet.Cells(37, 1)
CR.PasteSpecial()


Dim lastRow As Integer = xlWorkSheet.UsedRange.Rows.Count

Dim emptyRow As Integer = lastRow + 3

lastRow = xlWorkSheet.UsedRange.Rows.Count
'This is next emptyRow in the sheet
emptyRow = lastRow + 3
copyAlltoClipboard2()

Dim CR1 As Excel.Range = xlWorkSheet.Cells(emptyRow, 1)
CR1.PasteSpecial()


xlWorkBook.SaveAs("C:\weg\Verbruik1.xlsx")
xlWorkBook.Close()
End Sub
BASSIES 5-Feb-19 15:00pm    
Is there a better way , and not from a database.

1 solution

Why are you using the clipboard when you can copy data directly? See my suggestion at Re: Load xlsx filr from folder and write to new workbook - Visual Basic Discussion Boards[^]
 
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