Solved

Adding formula to range is taking too long using Access VBA syntax.

Posted on 2014-10-23
5
432 Views
Last Modified: 2014-10-23
Please note the following code.  At about the line:  For j = 2 To lLastRow I start to change the formulas for rows 2- 2000 in under column AR.   Please note I need to have the formula sum of AI through AP for each row under AR.   So I build this formula using for / each.  The problem is that it takes a little long to have the formula update for 2,000 rows - it can take 2-3 minutes to run.    Is there another syntax I can try that will update the range quicker?

  Dim xlApp As Object
  Dim xlBook As Object
  Dim xlSheet  As Object
  Dim sFormula As String  
  Dim rs As DAO.Recordset
  Dim j As Long
  Dim lLastRow As Long
 
  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Open(sFile)
  
  Set xlSheet = xlBook.Worksheets(1)
  xlSheet.Activate
  xlSheet.Application.Visible = False
  
  Set rs = Application.CurrentProject.Application.CurrentDb.OpenRecordset(sSQLExcel)
  With xlSheet
    .Range(sRange).copyfromrecordset rs
    .Range("AR:AZ").NumberFormat = "$#,##0.00;$-#,##0.00;"""""
    lLastRow = 2000
    
    For j = 2 To lLastRow
      sFormula = "=SUM(AI" & j & ":AP" & j & ")"
      sFormula = Replace(sFormula, " ", "")
      .Range("AR" & j).Formula = sFormula
    Next j
    
  End With
  rs.Close
  Set rs = Nothing

  Set xlSheet = Nothing
  xlBook.Save
  xlBook.Close
  Set xlBook = Nothing
  Set xlApp = Nothing

Open in new window

0
Comment
Question by:stephenlecomptejr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40400680
I added the code to disable calculations and screen updates until the code is completed then the calculation is turned on.


can you try the code now:

  Dim xlApp As Object
  Dim xlBook As Object
  Dim xlSheet  As Object
  Dim sFormula As String  
  Dim rs As DAO.Recordset
  Dim j As Long
  Dim lLastRow As Long
 
  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Open(sFile)
  
  Set xlSheet = xlBook.Worksheets(1)
  xlSheet.Activate
  xlSheet.Application.Visible = False
  ' I disabled the automatic calculations and screen and status bar updates in order to speed up the process
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
  
  Set rs = Application.CurrentProject.Application.CurrentDb.OpenRecordset(sSQLExcel)
  With xlSheet
    .Range(sRange).copyfromrecordset rs
    .Range("AR:AZ").NumberFormat = "$#,##0.00;$-#,##0.00;"""""
    lLastRow = 2000
    
    For j = 2 To lLastRow
      sFormula = "=SUM(AI" & j & ":AP" & j & ")"
      sFormula = Replace(sFormula, " ", "")
      .Range("AR" & j).Formula = sFormula
    Next j
    
  End With
  rs.Close
  Set rs = Nothing


  ' I enabled the automatic calculations and screen and status bar updates in order to speed up the process
Application.ScreenUpdating = True
Application.DisplayStatusBar = FTrue
Application.Calculation = xlCalculationAutomatic

  Set xlSheet = Nothing
  xlBook.Save
  xlBook.Close
  Set xlBook = Nothing
  Set xlApp = Nothing

Open in new window

0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40401030
Since everything is late-binded your code does not work.

For example the debug dies at Application.ScreenUpdating = True   <--- which is at the period ScreenUpdating with a Compile error:  Method or data member not found.
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40401040
Ok.  So I misspoke.  I just changed the one line of code that didn't implement late binding to this:
And I just added xlSheet. in front of the application code.

xlSheet.Application.ScreenUpdating = False
  xlSheet.Application.DisplayStatusBar = False
  xlSheet.Application.Calculation = -4135

Open in new window


xlSheet.Application.ScreenUpdating = True
  xlSheet.Application.DisplayStatusBar = True
  xlSheet.Application.Calculation = -4105

Open in new window


Thank you sincerely for your response!
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40401041
I've requested that this question be closed as follows:

Accepted answer: 500 points for samo4fun's comment #a40400680
Assisted answer: 0 points for stephenlecomptejr's comment #a40401040

for the following reason:

Just needed a tweak on the code but the idea worked well.  Thank you sincerely for replying!
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 40401042
Works great - need to give full points to this person's reply.  I appreciate it.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why this Update SQL not Updating! 15 53
Outlook VBA question - How do I mark a message as read 2 52
Outlook Automation in Access Using "Find" 2 53
Calculation in a Report 13 35
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question