Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large File Size Copy Ranges with VBA

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
like image 358
learningAsIGo Avatar asked Nov 27 '25 05:11

learningAsIGo


1 Answers

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.

enter image description here

like image 166
Eric K. Avatar answered Nov 29 '25 00:11

Eric K.