Results 1 to 5 of 5

Thread: Invalid Row Number

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    50

    Invalid Row Number

    Greetings!

    I have seen alot of code on this forum for exporting Datagrid data to excel, which is what I am trying to achieve. However, I have not been able successfully adapt any code to work. I keep getting an "Invalid Row Number". It looks like its going to work where the field name gets populated in cell A1 and then all the values for field one get populated but when it should iterate to B1, I get the error.

    Any ideas?
    Thanks,
    dolo

    Code:
    Private Sub cmdExport_Click()
    
    Dim Appli As New Excel.Application
    Dim C As Integer
    Dim F As Integer
    
        Appli.SheetsInNewWorkbook = 1
        Appli.Visible = True
        Appli.Workbooks.Add
    
            For C = 0 To DataGrid1.Columns.Count - 1
                DataGrid1.Col = C
                Appli.ActiveSheet.Cells(1, C + 1).Value = DataGrid1.Columns(C).Caption
                For F = 1 To DataGrid1.ApproxCount
                    Appli.ActiveSheet.Cells(F + 1, C + 1).Value = DataGrid1.Text
                    DataGrid1.Row = DataGrid1.Row + 1
                Next F
            Next C
    
    End Sub

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Invalid Row Number

    Took me a while. but I now see the problem.
    DataGrid1.Col = C sets the col that is going to be copied from.
    Appli.ActiveSheet.Cells(1, C + 1).Value = DataGrid1.Columns(C).Caption - this copies the col header I assume.
    For F = 1 To DataGrid1.ApproxCount -- loops through the rows....
    Appli.ActiveSheet.Cells(F + 1, C + 1).Value = DataGrid1.Text -- copies the data from the grid and puts it into the worksheet.
    DataGrid1.Row = DataGrid1.Row + 1 -- move to the next row....
    Next F -- keep looping

    What I don't see is that after or before looping through the rows..... reseting it back to the first row.

    After the looping through all the rows in the first col, when it tries to get the next col, the current cell is still on the last row. So, when this happens: DataGrid1.Row = DataGrid1.Row + 1 -- it's trying to go past the last row in the grid.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    50

    Re: Invalid Row Number

    Hello!

    Thanks for the explanation. Are you suggesting that I iterate from left to right row by row? If so, how would I go about doing that?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Invalid Row Number

    No... I was suggesting that you want want to RESET your row after you are done with each column..... Just before the For F line....

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    50

    Re: Invalid Row Number

    Tg,

    Please bear with me, I am new to this game. Could you show me in code what you mean by "RESET"?

    Thanks

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