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.
So what am I doing wrong here. Can the code work more efficiently?
Please I need your expertise on this.
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: