Consulting

Results 1 to 15 of 15

Thread: Is there a way to speed up my code which copy a heavily formula infested worksheet

  1. #1

    Is there a way to speed up my code which copy a heavily formula infested worksheet

    and creates a multiple new sheets? (cont'd title question)

    My vba code copies 1 hidden tab and creates a number of new sheets on that basis with a different name (name is specified by the user in a section of the excel workbook). The problem is that the hidden tab (that one which is copied) has got a significant number of formulas (about 100 rows x10 columns - most formulas a lookups/vlookups). It means creation of each new sheet take a lot of time - about 25 second each. I need to have calculation on as the results of formulas in each new tab are required for a further processing.
    The users i built it for complained that it is long time.
    I want to add that i already tried with switching on Application.Calculation = xlAutomatic at the end of the code but the results were pretty much identical as putting 'Calculate' in the middle as it is now.
    How can i speed up my vba procedure (i can add that i already use Application. property with a various actions switched off - as per below example)? OR is there no chance to improve speed if you worksheets are so heavily formula infested.

    
    Sub ConvertInput() ' put the user's input from the named range into the array
    Dim i As Integer
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    Application.StatusBar = False
    If wsToDuplicate.Visible = xlSheetVeryHidden Or wsToDuplicate.Visible = xlSheetHidden Then
        wsToDuplicate.Visible = xlSheetVisible
    End If
    myArray = Range("rng_Target").Value
    For i = LBound(myArray) To UBound(myArray)
        If IsEmpty(myArray(i, 1)) = False Then   'check if the element of myarray is empty or if there is any value there (string)
            wsToDuplicate.Copy After:=Sheets(Sheets.Count)
             With ActiveSheet
                  .name = myArray(i, 1)
             End With
         End If
         'This fragment is only to test whether array works
         'MsgBox myArray(i, 1)
         'Debug.Print myArray(i, 1)
    Next i  
    Erase myArray() ' Clear array
    Calculate
    If wsToDuplicate.Visible = xlSheetVisible Or wsToDuplicate.Visible = xlSheetHidden Then
       wsToDuplicate.Visible = xlSheetVeryHidden
    End If
    Call X
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    'Application.Calculation = xlCalculationAutomatic
    Application.StatusBar = True
    End Sub
    
    Sub X()
    Dim LastRemR As Integer
    For Each wrksh In ThisWorkbook.Worksheets
        LastRemR = 119
        If wrksh.name <> "TB" And wrksh.name <> "Lead - TO DELETE" And wrksh.name <> "Lead list & MAT" And _
        wrksh.name <> "Input" Then
            wrksh.Activate
            With wrksh.Range("M20:M119")
                  .Value = .Value
            End With
            With wrksh.Columns("M").SpecialCells(xlCellTypeConstants)
            LastR = .Cells(.Cells.Count).Row + 1
            End With
            Rows(LastR & ":" & LastRemR).Delete
            On Error GoTo Correct:
            'Check the last row again to extend the borders to the last cell of the table 
            With wrksh.Columns("M").SpecialCells(xlCellTypeConstants)
                 LastR = .Cells(.Cells.Count).Row
            End With
            GoTo Borders:
            Correct:
            LastR = 20
            Borders:
            wrksh.Range("B" & LastR & ":" & "L" & LastR).Borders.LineStyle = xlContinuous
            End If
        Next wrksh
    End Sub



    Last edited by Aussiebear; 01-21-2024 at 04:01 PM. Reason: Cleaned up presentation

  2. #2
    I think you should look into the formulas, can they be made more efficient? Can you share the formulas by any chance?
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    To be honest, I think JKP is on the money here. Copying time is not really the issue here. What we haven't seen is the formulas, how they work (read write access times etc) will I believe turn out to be the time user here.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Hello and thanks for your answers. Attached a copy of the file and screenshot with types of formulas used. Hope it helps.
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Pavlolova@; 01-22-2024 at 06:20 AM.

  5. #5
    I suggest to turn your data into a table (Format as Table). You can then rewrite your formulas so they only point to the data in the table and not to entire columns.
    If I copy the formula in cell B20 down for 10 rows, its calculation takes as much as 1 second. If I replace that formula
    =IFERROR(XLOOKUP(1,(TB!$A:$A=$A20)*(TB!$B:$B=$D$8),TB!$C:$C),"")
    with this one:
    =IFERROR(XLOOKUP(1,(TB!$A$2:$A$54=$A20)*(TB!$B$2:$B$54=$D$8),TB!$C$4:$C$54),"")
    The calculation time for those 10 cells drops to 0.00003 seconds (!)
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. wsToDuplicate doesn't seem to be set anywhere in the code from #1 that I added to your attachment. If there are other macros, it'd be better ti include them and attach an XLSM file

    2. I had a hard time following the logic (not my style) so maybe a verbal explaination of the steps might help.

    3. Do the output files require the formulas, or just the processed data?

    4. One approach that I've used is

    a. Include a hidden empty WS as a 'template' with formatting, etc. (e.g. "_Hidden to delete") in WB-1
    b. Make the hidden WS(s) visible and copy to new WB with final name (e.g. "Hidden to delete") WB-2
    c. Make the now visible WS's in WB-1 not visible
    d. Load an array with the source data (e.g. TB)
    e. Loop the array and test contents to see if it should be included on one of the WB-2 WS's
    f. Copy and put the needed values into the WS or WS's in WB-2, i.e. no formulas
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    I am really struggling to place any response on this thread - constantly see this message:
    1. Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words.


    Even if there is no URLs or any offensive words used.

  8. #8
    LG_v.17Copy - Copy.xlsm I have attached the whole model now with accompanying code so it should be easier to understand what it does.

  9. #9
    wsToDuplicate is a reference to a hidden worksheet that is hidden and is used as a template for the creation of the new worksheets.

  10. #10

  11. #11
    Have you considered my advice at all?
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    @Pavlolova@. Any particular reason why you failed to mention that you have already posted this same issue on other forums?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    JKP , my posts to you are totally denied. I have no patience or will to create another one (spending 5min writing the answer just to be denied by the algorithm on this forum indicating my answer is offensive (sic!)). Sorry, I gave up.
    Last edited by Pavlolova@; 01-23-2024 at 04:29 AM.

  14. #14
    Pavlova: odd!
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  15. #15
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Since the OP has failed to respond to my question, nor have they indicated this issue has been posted elsewhere, this thread is now closed.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Tags for this Thread

Posting Permissions

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