I have a routine that duplicates a sheet by generating cell references to the old sheet. My problem is for that when I run this routine on a three sheets in a workbook sized 280KB it balloons up to 80MB. If I copy and paste the sheet values to remove the formulas, the workbook stays the same size. If I delete the sheets and save then it returns to it's original size. Is there something I'm missing in the way VBA handles memory that is causing this issue? I've already tried setting all of the variables to nothing at the end of the routine.
Public Sub CopySheetFormulas(inputSheet As Worksheet, outputSheet As Worksheet)
Dim rowCounter As Long
Dim columnCounter As Long
Dim maxRow As Long
Dim maxColumn As Long
maxColumn = inputSheet.Cells.Find(What:="*", After:=inputSheet.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
Debug.Print "Max Column: " & maxColumn
maxRow = inputSheet.Cells.Find(What:="*", After:=inputSheet.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
Debug.Print "Max Row: " & maxRow
inputSheet.Range("A1").Resize(maxRow, maxColumn).Copy
With outputSheet
.Activate
.Range("A1").Select
.Paste Link:=True
End With
End Sub
If you want to reduce the file size, you need to delete the additional row or column from the sheet, then save it. File size will reduce.
For example below snapshot, previously i have 22 records, although i have removed some record, but the empty cells still contributing the file size. How to know it, CTRL + End, the cell it stop will tell you.
Remove those empty Rows or Columns will reduce file size.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With