+ Reply to Thread
Results 1 to 10 of 10

Help/Advise to optimize Excel - SQL data retrieval

  1. #1
    Registered User
    Join Date
    06-20-2022
    Location
    Singapore
    MS-Off Ver
    18.2205.1091.0
    Posts
    5

    Help/Advise to optimize Excel - SQL data retrieval

    Firstly, appreciate all the help I can get as I am not an expert in this field and am really struggling to resolve this for work.

    Background, there is this excel macro that will extract data from sql. The most rows I have seen by far would be around 8k. I need to extract a month worth of data. Currently, my excel will hang or crash when I try to run the excel. I am able to generate the data if I choose a time range of 2 weeks.

    I believe that my coding might be inefficient or that there could be certain settings I can change to improve this timing and successfully generate a month worth of data.

    I have attached the codes below.

    Thank you in advance.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    06-20-2022
    Location
    Singapore
    MS-Off Ver
    18.2205.1091.0
    Posts
    5

    Re: Help/Advise to optimize Excel - SQL data retrieval

    Attachment 784370

    Attached a sample image.

  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Help/Advise to optimize Excel - SQL data retrieval

    Just some thoughts - no idea if they will make any difference to your issue, but...

    I'd use
    Please Login or Register  to view this content.
    Rather than the loops across the columns until EOF. I would also do the date check (< 2000-01-01) in the stored procedure rather than Excel.

    Likewise, if you want to clear a range then
    Please Login or Register  to view this content.
    ...rather than 100,000 loops clearing individual rows.

    Finally, I'd close each recordset when finished with it, and just for completeness, set the variable to nothing at the end of each procedure. Would also consider explicitly closing the connection and destroying the variables as well. SQL in Excel can leak memory.
    Last edited by cytop; 06-20-2022 at 03:00 AM.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Help/Advise to optimize Excel - SQL data retrieval

    Nothing looks particularly wrong to me - are the stored procedures just slow? Have you tried running them in SSMS and seeing how long they take?

  5. #5
    Forum Contributor
    Join Date
    02-25-2022
    Location
    Dallas, Texas
    MS-Off Ver
    Office 365
    Posts
    174

    Re: Help/Advise to optimize Excel - SQL data retrieval

    I don't use SQLOLEDB.1 to connect.

    Please Login or Register  to view this content.
    I use this to connect to Azure SQL Server

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-20-2022
    Location
    Singapore
    MS-Off Ver
    18.2205.1091.0
    Posts
    5

    Re: Help/Advise to optimize Excel - SQL data retrieval

    Quote Originally Posted by Kyle123 View Post
    Nothing looks particularly wrong to me - are the stored procedures just slow? Have you tried running them in SSMS and seeing how long they take?

    Hmm. Actually the database is about 15GB. Previously we were still able to load a full month in about 20 minutes. But after some security upgrades seems like query time out happens.
    Last edited by CuBixLoNneR; 06-20-2022 at 12:43 PM.

  7. #7
    Registered User
    Join Date
    06-20-2022
    Location
    Singapore
    MS-Off Ver
    18.2205.1091.0
    Posts
    5

    Re: Help/Advise to optimize Excel - SQL data retrieval

    Quote Originally Posted by cytop View Post
    Just some thoughts - no idea if they will make any difference to your issue, but...

    I'd use
    Please Login or Register  to view this content.
    Rather than the loops across the columns until EOF. I would also do the date check (< 2000-01-01) in the stored procedure rather than Excel.

    Likewise, if you want to clear a range then
    Please Login or Register  to view this content.
    ...rather than 100,000 loops clearing individual rows.

    Finally, I'd close each recordset when finished with it, and just for completeness, set the variable to nothing at the end of each procedure. Would also consider explicitly closing the connection and destroying the variables as well. SQL in Excel can leak memory.


    How should I use the "Cells(row_idx, 1).CopyFromRecordset". Sorry I'm not very well versed in VBA.

  8. #8
    Registered User
    Join Date
    06-20-2022
    Location
    Singapore
    MS-Off Ver
    18.2205.1091.0
    Posts
    5

    Re: Help/Advise to optimize Excel - SQL data retrieval

    Further I have tried changing "Query options > Data Load > Background data > Never allow data previews to download in the background" from an old thread in this forum to optimize Excel to SQL data loading. Any other options that will help improve the loading speed.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Help/Advise to optimize Excel - SQL data retrieval

    Can’t you just create an agent job that runs the stored procedures and dump the output to a table, then query that in Excel?

  10. #10
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Help/Advise to optimize Excel - SQL data retrieval

    Quote Originally Posted by CuBixLoNneR View Post
    How should I use the "Cells(row_idx, 1).CopyFromRecordset". Sorry I'm not very well versed in VBA.
    CopyFromRecordset dumps a recordset object into a worksheet starting at whichever cell you specify - saves the 2 loops to first copy each field across the columns and then each record into rows

    An example straight from the MS Help page for CopyFromRecordset. This writes the field names into cells in Row 1 (and sets the font to bold) and then copies the recordset starting into cell A2. No need to loop the fields and records.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel Invoice data retrieval Code/macro in VBA
    By Mark Dynes in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-09-2019, 01:02 PM
  2. [SOLVED] Retrieval of original data inputs in Excel
    By elsaedv in forum Excel General
    Replies: 10
    Last Post: 09-16-2012, 08:14 PM
  3. excel 2007 - filtered data retrieval from sql server
    By MarMo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-25-2012, 02:38 AM
  4. Excel data retrieval for pizzeria
    By sheitmar in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-30-2008, 01:41 AM
  5. Excel and DB Table Data Retrieval
    By Talbot in forum Excel General
    Replies: 1
    Last Post: 03-16-2007, 08:45 AM
  6. Excel Form and Data Retrieval
    By Johnny New Englander in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-04-2006, 04:25 PM
  7. [SOLVED] retrieval of data from web to excel
    By chits in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-04-2005, 10:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1