Results 1 to 2 of 2

Thread: Increment Row after paste from clipboard

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    Increment Row after paste from clipboard

    Help please... I am wanting to put the data from a datagridview into an excel spread sheet.
    I can to the headers and all correectly and they get placed on Row 2

    However in my datagrid loop

    RowNum = 3
    NumRecs = DataGridView1.Rows.Count.ToString()
    Rnum = 0
    For Each r In DataGridView1.Rows
    TheRange = "A" + (RowNum.ToString & ",Z")
    xlSheet.Range(TheRange, TheRange).Select()

    WrkStr = r.Cells("NameOut").Value.ToString()
    WrkStr = WrkStr + vbTab + r.Cells("EMAilOut").Value.ToString()
    WrkStr = WrkStr + vbTab
    Clip.SetDataObject(WrkStr)
    xlSheet.Paste(misValue, misValue)
    RowNum = RowNum + 1
    Rnum = Rnum + 1
    If Rnum = (NumRecs - 1) Then
    Exit For
    End If
    Next
    xlRange = xlSheet.Columns
    xlRange.AutoFit()

    EVERYLINE line added is in row 1 ..... How do I get it to go row 4 thru row xxxxx ?????

    Thanks

    gollnick
    William E Gollnick

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Increment Row after paste from clipboard

    you can try like
    Code:
    RowNum = 3
    NumRecs = DataGridView1.Rows.Count.ToString()
    Rnum = 0
    For Each r In DataGridView1.Rows
          WrkStr = r.Cells("NameOut").Value.ToString()
          WrkStr = WrkStr + vbTab + r.Cells("EMAilOut").Value.ToString()
          WrkStr = WrkStr + vbTab
          Clip.SetDataObject(WrkStr)
          xlSheet.cells(rownum, 1).Pastespecial
         RowNum = RowNum + 1
         Rnum = Rnum + 1
         If Rnum = (NumRecs - 1) Then
              Exit For
        End If
    Next
    xlRange = xlSheet.Columns
    xlRange.AutoFit()
    though there are several variables that i am unsure what are used for
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width