2 mb of file converts into 10 mb file after VBA runs...

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hello Excel Gurus,

How are you today? I need your help in fixing this problem that converts my excel file from just 2mb into 10 mb file. I have some luck in pin pointing the line which is the main culprit . It will also take more time when running that line and will increase the size of the file simultaneosly. By removing the said line the code works faster and file remains 2mb as same in the original file.

The line is highlited in the code below.

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.EnableAnimations = False
Dim answer As Integer
Dim Path As String
Dim FileName As String
Dim Ws As Worksheet
Dim ans1 As Long
Dim ans2 As Long
Dim i As Long
ans1 = ThisWorkbook.Sheets.Count
ans2 = ThisWorkbook.Sheets.Count

On Error Resume Next
If Not Intersect(Target, Range("A2")) Is Nothing Then
answer = MsgBox(" This will execute inventory close as of " & Worksheets("HSheet").Range("B23").Value & "." & _
vbNewLine & vbNewLine & " Are you sure you want to continue?", vbExclamation + vbYesNo + vbDefaultButton2, "Confirmation")
        If answer = vbYes Then
    For i = 1 To ans1
        Select Case Sheets(i).Name
            Case "HSheet", "SS"
                'do nothing
            Case Else
                Sheets(i).Unprotect "abc123"
                Sheets(i).Range("AH5:HZ700").Locked = True
                Sheets(i).Protect "abc123"
        End Select
    Next
Worksheets("SS").Select
ThisWorkbook.Save

Path = Worksheets("HSheet").Range("B18")
FileName = Worksheets("HSheet").Range("B24")
ActiveWorkbook.SaveAs FileName:=Path & FileName & ".xlsm"
    For i = 1 To ans2
        Select Case Sheets(i).Name
            Case "HSheet", "SS"
                'do nothing
            Case Else
                Sheets(i).Unprotect "abc123"
                Sheets(i).Range("AH5:HZ700").ClearContents
                Sheets(i).Range("AH5:HZ700").Locked = False'<<<<<<<<<<<<<<<<< This line is the culprit.
                Sheets(i).Protect "abc123"
        End Select
    Next
Worksheets("SS").Select
Worksheets("HSheet").Range("B11") = ThisWorkbook.FullName
ThisWorkbook.Save

MsgBox "The operation completed successfully." & vbNewLine & vbNewLine & Worksheets("HSheet").Range("B23").Value & _
" stock file is created for you..", vbInformation, "Information"
        End If
End If
Cancel = True
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Application.EnableAnimations = True
Application.Calculation = xlCalculationAutomatic
End Sub

So what am I doing wrong here. Can the code work more efficiently?
Please I need your expertise on this.
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
And if I increase the range on below line from HZ700 to HZ800 then around 1.3 mb is further added to filesize of 10.4 mb giving me 11.7 mb of total filesize.
VBA Code:
               Sheets(i).Range("AH5:HZ700").Locked = False

Anyone?
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,288
Members
448,885
Latest member
LokiSonic

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top