|
-
Aug 24th, 2004, 01:35 PM
#1
Thread Starter
Lively Member
Generate Excel sheet through coding in VB?
Hello,
In my application i am generating execel sheet through coding. But it makes my application very slow.
is there any other more efficient and optimized way to do this.?
is it possible to run pre-defined macro in VB at runtime which accept data and generate excel sheet?
All inputs are welcomed.
Thanks in advance.
HDave
~~~......Hope never dies. one has to work for it to make it reality.......~~~
-
Aug 24th, 2004, 02:13 PM
#2
This should probably go in the VBA forum, but you may save time
by creating a template formatted as you want. Then you only
have to populate it with the data.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Aug 24th, 2004, 02:24 PM
#3
Thread Starter
Lively Member
Hey,
Templates means one procedure in VB which generate excel sheet in certain format without data?
In my case no of data items is changing. So , templates of excel sheet has to chage according to no. of data items.
I will also try in VBA Section.
HDave
~~~......Hope never dies. one has to work for it to make it reality.......~~~
-
Aug 24th, 2004, 03:55 PM
#4
By templates I think that RobDog888 means a pre-made Excel sheet without data, but with all other formatting. Then in your code yuo can simply copy the file and open it rather than making a new workbook.
This is a good idea, as it is actually the slowest part of the process (assuming the rest of the code is efficient!). For my programs formatting is generally around 50-70% of the creation time, but I cannot use templates because of various deployment issues.
is it possible to run pre-defined macro in VB at runtime which accept data and generate excel sheet?
I'm pretty sure you can, but it isn't noticably different in terms of speed.
SO, the importan question: how are you filling the sheet? Are you using something like Sheet.Cells(x,y) = "my text" ? If so, the chances are you can make the code take less than a third of the time it does at the moment (and maybe much less).
The .Cells() method is extremly slow, as it forces Excel to do its recalculating/reformating after every cell, rather than after a group of them.
If you are getting the data from a recordset, use the .CopyFromRecordset method that Excel provides - nothiing else you can do is faster.
If the data isn't from a recordset, you can fill the sheet using an array instead, eg:
VB Code:
Dim MyArray(2,3)
MyArray(1,1) = "test"
MyArray(1,2) = "this"
MyArray(1,3) = "speedy"
MyArray(2,1) = "little"
MyArray(2,2) = "beauty"
MyArray(2,1) = "!!!!"
'change xlSheet to the name of your sheet object
xlSheet.Range("A2:B4") = MyArray
(nb: this is from memory, there may be minor bugs!)
oh, and as you have variable amounts of data you will probably want to set different ranges (eg: A2:E54 and A2:F101). to set different ranges search for I funtion I made called xlcol or xl_col on this forum
Last edited by si_the_geek; Aug 24th, 2004 at 04:01 PM.
-
Aug 25th, 2004, 09:57 AM
#5
Thread Starter
Lively Member
Thanks!!! {RESOLVED}
I got it now.
~~~......Hope never dies. one has to work for it to make it reality.......~~~
-
Sep 7th, 2004, 06:09 PM
#6
Re: Generate Excel sheet through coding in VB?
Originally posted by hdave
Hello,
In my application i am generating execel sheet through coding. But it makes my application very slow.
is there any other more efficient and optimized way to do this.?
is it possible to run pre-defined macro in VB at runtime which accept data and generate excel sheet?
All inputs are welcomed.
Thanks in advance.
HDave
I ran into this problem creating Excel spreadsheets via VB6 also.
I found that certain operations (dereferencing Range objects, inserting PageBreaks, and any general visual formatting) take extremely long and can drag your running time into the mud if placed hap-hazzardly in your loops.
Things that are fast are "data entry", where you are throwing data into cells, referenced directly from A1 on the worksheet.
For example, I made a reporting program that generated about a thousand pages in Excel. It took 40 hours to complete.
I dug around and made the above optimizations and got it down to about a half-hour.
If you are willing to spend some quality time with your code, you will find you can significantly reduce your running time.
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
|