|
-
Aug 18th, 2005, 12:46 PM
#1
Thread Starter
Member
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
-
Aug 18th, 2005, 01:02 PM
#2
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
-
Aug 18th, 2005, 01:17 PM
#3
Thread Starter
Member
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?
-
Aug 18th, 2005, 01:51 PM
#4
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
-
Aug 18th, 2005, 02:19 PM
#5
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|