Brad Friedman
New Member
- Joined
- Aug 21, 2020
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
I have code that opens an excel file, goes sheet by sheet row by row looking to see if cell Cx is not blank (x is the row #). If it is not blank, it copies some of the cells on that row to another open Workbook. It performs this task for every file in a folder. The problem is not the number of files, or the number of sheets or the number of rows. The problem is that each record written takes literally 4 seconds to write out a 5 cell record (2 assigned values, 3 copied from the worksheet in question). When you have 600 rows that need to get written out, thats like 41 minutes (40 minutes to write the records, and no time to traverse the 3000+ records that are not written out...
I have done the obvious stuff like turn off screen updating and auto calculation - instead of copy/paste (.Range assignment), I have tried Cell and offset commands too - they are actually worse in performance. The time is definitely in the "copy" with the Range assignment area - everything else runs "normally"...
I highlighted the slow mover...Any help is appreciated! I am sure there has to be something faster...
'Optimize Macro Speed
Application.StatusBar = ""
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
'Removed code irrelevant here
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
Application.ScreenUpdating = True
Application.StatusBar = "Working on " & myFile & " Be Patient - this is a slow process"
Application.ScreenUpdating = False
' Remove old comments from this person
wb.Sheets("Instructions").Select
lbtext = wb.Sheets("Instructions").Range("D1")
i = 8
ThisWorkbook.Sheets("Comments").UsedRange ''Refresh UsedRange
Do While i <= ThisWorkbook.Sheets("Comments").UsedRange.Rows(ThisWorkbook.Sheets("Comments").UsedRange.Rows.Count).Row
If (ThisWorkbook.Sheets("Comments").Range("A" & i).Value = lbtext) Then
ThisWorkbook.Sheets("Comments").Rows(i).Delete
Else
i = i + 1
End If
ThisWorkbook.Sheets("Comments").UsedRange 'Refresh UsedRange
Loop
lbtext = ""
For i = 1 To 12
codenameshname = "Ven_" & i
Set shname = GetSheetWithCodename(codenameshname, wb, lbtext)
For j = 12 To 260
If (shname.Range("C" & j) <> "") Then
With ThisWorkbook.Sheets("Comments")
.UsedRange 'Refresh UsedRange
lastrow = .UsedRange.Rows.Count + 2
.Range("A" & lastrow) = lbtext
.Range("B" & lastrow) = Date
.Range("C" & lastrow) = shname.Range("B11").Value
.Range("D" & lastrow) = shname.Range("A" & j).Value
.Range("E" & lastrow) = shname.Range("C" & j).Value
End With
End If
Next j
Next i
'Save and Close Workbook
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Comments Copied!"
End If
I have done the obvious stuff like turn off screen updating and auto calculation - instead of copy/paste (.Range assignment), I have tried Cell and offset commands too - they are actually worse in performance. The time is definitely in the "copy" with the Range assignment area - everything else runs "normally"...
I highlighted the slow mover...Any help is appreciated! I am sure there has to be something faster...
'Optimize Macro Speed
Application.StatusBar = ""
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
'Removed code irrelevant here
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
Application.ScreenUpdating = True
Application.StatusBar = "Working on " & myFile & " Be Patient - this is a slow process"
Application.ScreenUpdating = False
' Remove old comments from this person
wb.Sheets("Instructions").Select
lbtext = wb.Sheets("Instructions").Range("D1")
i = 8
ThisWorkbook.Sheets("Comments").UsedRange ''Refresh UsedRange
Do While i <= ThisWorkbook.Sheets("Comments").UsedRange.Rows(ThisWorkbook.Sheets("Comments").UsedRange.Rows.Count).Row
If (ThisWorkbook.Sheets("Comments").Range("A" & i).Value = lbtext) Then
ThisWorkbook.Sheets("Comments").Rows(i).Delete
Else
i = i + 1
End If
ThisWorkbook.Sheets("Comments").UsedRange 'Refresh UsedRange
Loop
lbtext = ""
For i = 1 To 12
codenameshname = "Ven_" & i
Set shname = GetSheetWithCodename(codenameshname, wb, lbtext)
For j = 12 To 260
If (shname.Range("C" & j) <> "") Then
With ThisWorkbook.Sheets("Comments")
.UsedRange 'Refresh UsedRange
lastrow = .UsedRange.Rows.Count + 2
.Range("A" & lastrow) = lbtext
.Range("B" & lastrow) = Date
.Range("C" & lastrow) = shname.Range("B11").Value
.Range("D" & lastrow) = shname.Range("A" & j).Value
.Range("E" & lastrow) = shname.Range("C" & j).Value
End With
End If
Next j
Next i
'Save and Close Workbook
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Comments Copied!"
End If