Hello,
So I have this code, written by someone else, that is working but makes my spreadsheet freeze everytime it loads the new image (3sec if I let it as is in the code). I can't scroll up/down at that specific moment, but once the new background image is loaded it is fine.
I would like to optimize and remove the freezes from Excel if possible or diminish it a lot in the worst case.
I do not understand the code itself, it is way out of my understanding.
Thank you
ThisWorkbook
Module11
So I have this code, written by someone else, that is working but makes my spreadsheet freeze everytime it loads the new image (3sec if I let it as is in the code). I can't scroll up/down at that specific moment, but once the new background image is loaded it is fine.
I would like to optimize and remove the freezes from Excel if possible or diminish it a lot in the worst case.
I do not understand the code itself, it is way out of my understanding.
Thank you
ThisWorkbook
VBA Code:
Private Sub Workbook_Open()
...
mainbg = True
Pause = False
Call file_names
Call Macro1
...
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call Macro2
...
End Sub
Module11
VBA Code:
Public mainbg As Boolean
Option Explicit
Const pth = "C:\Personnel\Backgrounds\"
Const mfPttrn = "*.jpg"
Private i As Integer, rn As Integer
Private TimeToRun As Date
Private fleTbl() As String
VBA Code:
Sub file_names()
mainbg = True
Dim fle As String
i = CreateObject("Scripting.FileSystemObject").GetFolder(pth).Files.Count
ReDim fleTbl(1 To i, 1 To 2)
i = 0
fle = Dir(pth & mfPttrn, vbNormal)
Do Until fle = ""
If mainbg = False Then GoTo LabelEnd
i = i + 1
fleTbl(i, 1) = i
fleTbl(i, 2) = pth & fle
fle = Dir()
Loop
LabelEnd:
End Sub
VBA Code:
Sub Macro1()
If mainbg = False Then GoTo LabelEnd
Randomize
rn = Int(UBound(fleTbl, 1) * Rnd + 1)
ActiveSheet.SetBackgroundPicture Filename:=fleTbl(rn, 2)
TimeToRun = Now + TimeValue("00:00:03")
Application.OnTime EarliestTime:=TimeToRun, Procedure:="Macro1"
LabelEnd:
End Sub
VBA Code:
Public Function FilenameFromPath(path As String) As String
Dim S As String
Dim V As Variant
' Make sure all separators are the same
path = Replace(path, "/", "\")
V = Split(path, "\")
FilenameFromPath = V(UBound(V))
End Function
VBA Code:
Sub Macro2()
On Error Resume Next
Application.OnTime EarliestTime:=TimeToRun, Procedure:="Macro1", Schedule:=False
ActiveSheet.SetBackgroundPicture Filename:=""
fleTbl = Empty
End Sub