PDA

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



Pavlolova@
01-21-2024, 05:04 AM
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

Jan Karel Pieterse
01-22-2024, 03:40 AM
I think you should look into the formulas, can they be made more efficient? Can you share the formulas by any chance?

Aussiebear
01-22-2024, 04:06 AM
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.

Pavlolova@
01-22-2024, 05:10 AM
Hello and thanks for your answers. Attached a copy of the file and screenshot with types of formulas used. Hope it helps.

Jan Karel Pieterse
01-22-2024, 07:00 AM
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 (!)

Paul_Hossler
01-22-2024, 07:25 AM
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

Pavlolova@
01-22-2024, 01:37 PM
I am really struggling to place any response on this thread - constantly see this message:

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.

Pavlolova@
01-22-2024, 01:54 PM
31305 I have attached the whole model now with accompanying code so it should be easier to understand what it does.

Pavlolova@
01-22-2024, 01:56 PM
wsToDuplicate is a reference to a hidden worksheet that is hidden and is used as a template for the creation of the new worksheets.

Dave
01-22-2024, 02:04 PM
Is there a way to speed up my code which copy a heavily formula infested worksheet and creates a multiple new sheets? - Excel VBA / Macros - OzGrid Free Excel/VBA Help Forum (https://forum.ozgrid.com/forum/index.php?thread/1233630-is-there-a-way-to-speed-up-my-code-which-copy-a-heavily-formula-infested-workshe/)
Please review the forum guidelines. Dave

Jan Karel Pieterse
01-22-2024, 02:49 PM
Have you considered my advice at all?

Aussiebear
01-22-2024, 09:13 PM
@Pavlolova@. Any particular reason why you failed to mention that you have already posted this same issue on other forums?

Pavlolova@
01-23-2024, 03:37 AM
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.

Jan Karel Pieterse
01-23-2024, 04:00 AM
Pavlova: odd!

Aussiebear
01-23-2024, 09:52 PM
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.