Shloime
Board Regular
- Joined
- Oct 25, 2023
- Messages
- 60
- Office Version
- 365
- 2021
- 2019
- 2016
- Platform
- Windows
I have some macros in an excel file and they became extremely slow,
I added a few lines debug.print now to see where it is taking long,
I use this to find the last row/coplumn:
Range("o:v").Find(What:="*", _ after:=Range("o1"), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).row.
I found that is taking 2 to 3 seconds,
it is was better when i changed it to:
Range("o1:v1111").Find(What:="*", _ after:=Range("o1"), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).row.
and maybe even
Range("o1:v" & rows.count).Find(What:="*", _ after:=Range("o1"), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).row.
does it make sense Range("o1:v" & rows.count) to be more efficient than Range("o:v")?
I also have another macro to save or print data in a few columns, and this one started to slow down everything till i close the file,
this is the full macro to save
Sub PageSetupAndSave()
Dim svrg As Range
Dim Fold As String
finrw = stmn.Range("o:v").Find(What:="*", after:=Range("o1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).row
Application.ScreenUpdating = False '
Range("5:5").RowHeight = 36.75
Range("p").ColumnWidth = 17
Range("q:q").ColumnWidth = 17
Range("r:r").ColumnWidth = 9
Range("s:s").ColumnWidth = 9
Range("t:t").ColumnWidth = 10.35
Range("u:u").ColumnWidth = 25
Set svrg = Range("p1:u" & finrw + 1)
svrg.Select
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
'.PrintQuality = 600
.Orientation = xlPortrait
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Dim Folder As String
Folder = ThisWorkbook.Path & "\" & Format(Now, "yyyymmddhh") & "\"
Fold = Dir(Folder, vbDirectory)
If Fold = "" Then MkDir Folder
'folder = ThisWorkbook.Path & "\" & Format(Now, "yyyymmddhhmmss") ' ThisWorkbook.Path & "\Statements02"
FilNM = Replace(Folder & Range("b8") & Chr(32) & Format(Now, "yymmddhhmm") & Chr(32) & Range("b1").Value & ".pdf", """", "")
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FilNM, OpenAfterPublish:=False
Application.ScreenUpdating = True
End Sub
I added a few lines debug.print now to see where it is taking long,
I use this to find the last row/coplumn:
Range("o:v").Find(What:="*", _ after:=Range("o1"), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).row.
I found that is taking 2 to 3 seconds,
it is was better when i changed it to:
Range("o1:v1111").Find(What:="*", _ after:=Range("o1"), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).row.
and maybe even
Range("o1:v" & rows.count).Find(What:="*", _ after:=Range("o1"), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).row.
does it make sense Range("o1:v" & rows.count) to be more efficient than Range("o:v")?
I also have another macro to save or print data in a few columns, and this one started to slow down everything till i close the file,
this is the full macro to save
Sub PageSetupAndSave()
Dim svrg As Range
Dim Fold As String
finrw = stmn.Range("o:v").Find(What:="*", after:=Range("o1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).row
Application.ScreenUpdating = False '
Range("5:5").RowHeight = 36.75
Range("p").ColumnWidth = 17
Range("q:q").ColumnWidth = 17
Range("r:r").ColumnWidth = 9
Range("s:s").ColumnWidth = 9
Range("t:t").ColumnWidth = 10.35
Range("u:u").ColumnWidth = 25
Set svrg = Range("p1:u" & finrw + 1)
svrg.Select
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
'.PrintQuality = 600
.Orientation = xlPortrait
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Dim Folder As String
Folder = ThisWorkbook.Path & "\" & Format(Now, "yyyymmddhh") & "\"
Fold = Dir(Folder, vbDirectory)
If Fold = "" Then MkDir Folder
'folder = ThisWorkbook.Path & "\" & Format(Now, "yyyymmddhhmmss") ' ThisWorkbook.Path & "\Statements02"
FilNM = Replace(Folder & Range("b8") & Chr(32) & Format(Now, "yymmddhhmm") & Chr(32) & Range("b1").Value & ".pdf", """", "")
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FilNM, OpenAfterPublish:=False
Application.ScreenUpdating = True
End Sub