CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2019
    Posts
    3

    Excel Interop Help

    Hey All,

    I am having an issue returning an entire row of data rather then an individual cell. What am I doing wrong?

    Code:
                For i As Integer = 2 To counta
                    If CStr(xlWorkSheet2.Cells(i, 21).value) = Form3.Label1.Text Then
                               t1 = CStr(xlWorkSheet2.Cells(i, xlWorkSheet2.Range(i).EntireRow).Value)
                               xlWorkSheet2.
                               Form3.DataGridView1.Rows.Add(t1)
                               TextBox2.Text = "Row " & i
                               ProgressBar1.Value = ProgressBar1.Value + 1
                    Else
                               ProgressBar1.Value = ProgressBar1.Value + 1
                    End If
                Next
    Is there a way to return an entire row of data instead of just a cell?

    Thanks!
    Last edited by 2kaud; January 18th, 2019 at 05:41 AM. Reason: formatting

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Excel Interop Help

    Looks like the row you add only has one column (i.e. cell). I don't see code where you try to read.

    Can you open an excell file and read the rows (search google for an example)? Try this to figure out if the issue is with how you add the row rather than reading a row.

  3. #3
    Join Date
    Jan 2019
    Location
    Java 1.8
    Posts
    12

    Re: Excel Interop Help

    Copying an entire row and pasting into another worksheet is not big deal. However, if I'm understanding correctly, this will work to capture an entire row as text values and allow you to paste them into a document. You would replace the hard coding of "U7" with your Form Label value and then this will search row i column 21 (i, 21). This is a mock-up to show you. I couldn't attach the workbook as an example.

    Code:
    Dim i As Integer
    Dim intRow As Integer
    Dim intCol As Integer
    Dim rng As Variant
    Dim str As String
    Dim arr() As Variant
    
    intRow = Range("A1").End(xlToRight).Column
    intCol = Range("A1").End(xlDown).Row
    
    For i = 1 To intRow
        
        If Cells(i, 21) = "U7" Then
            arr = Rows("1:1").Value
             For Each rng In arr
                str = str & " " & rng
            Next
            MsgBox str
            
            End
        End If
    Next
    Name:  Excel example.jpg
Views: 822
Size:  20.2 KB
    Last edited by 2kaud; January 18th, 2019 at 05:42 AM.

  4. #4
    Join Date
    Jan 2019
    Location
    Java 1.8
    Posts
    12

    Re: Excel Interop Help

    Oops, replace the following... sorry.

    Replace: arr = Row(1:1).Value

    With arr = Rows(i & ":" & i).Value

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

Featured