Friday, April 2, 2010

Very Fast Loading Of Record Set Into Excel

Very Fast Loading Of Record Set Into Excel

Sub RsToExcel(sheet,rs)
Dim fieldNames,i
rs.MoveFirst
Redim fieldNames(rs.Fields.Count - 1)

For i=0 To rs.Fields.Count -1
fieldNames(i)=rs.Fields(i).Name
Next

mySheet.Range(mySheet.Cells(1,1),mySheet.Cells(1,rs.Fields.Count)).Value=fieldNames

For i=1 To rs.Fields.Count
mySheet.Columns(i).AutoFit
Next

mySheet.Cells.CopyFromRecordSet rs
mySheet.Rows(1).Insert
mySheet.Range(mySheet.Cells(1,1),mySheet.Cells(1,rs.Fields.Count)).Value=fieldNames
End Sub

[Via]