Insert Blank Rows in Excel

oralb5
09-22-2004, 01:28 PM
I am exporting a datagrid/datasource from my application using the code below. However, the user will be allowed to determine the number of records to select so I was wondering if anybody knew of a way to insert blank rows into a spreadsheet before I proceed with the export of the data.

Any ideas/suggestions would be appreciated. Thanks.


Dim excelApp As Excel.Application
Try
excelApp = GetObject(, "Excel.Application")
Catch ex As Exception
excelApp = New Excel.Application
End Try
Dim excelBook As Excel.Workbook = excelApp.Workbooks.Open(s)
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
With excelWorksheet
Dim dr As DataRow
Dim dc As DataColumn
z = -1
For Each dc In d.Tables(grid.DataSource.ToString).Columns
z += 1
i = 3
For Each dr In d.Tables(grid.DataSource.ToString).Rows
.Range(Chr(66 + z) & i.ToString).Value = dr(z)
i += 1
If i = 20 Then Exit For
Next
Next
End With
excelApp.Visible = True
excelWorksheet = Nothing
excelBook = Nothing
excelApp = Nothing
GC.Collect()

herilane
09-22-2004, 01:44 PM
Example - inserts n rows before row 5 (in VBA, so you'll have to make some adjustments):
Dim n As Integer
n = Application.InputBox("Number of rows to insert:", Type:=1)
ActiveSheet.Range("5:" & CStr(5 + n - 1)).EntireRow.Insert

oralb5
09-22-2004, 02:25 PM
Perfect...I figured it was something along these lines but couldn't get it exactly...thanks.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum