OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 421
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for any assistance. Is there a simpler faster way to perform the following task? I am finding the last row in a sheet which contains around 100k rows, then comparing the cells in each of two rows and performing a function. This code takes about 42 second to run.
VBA Code:
Option Explicit
'***************************************************************************************************************
Public Sub Test()
'_______________________________________________________________________________________________________________
'Turn off alerts, screen updates, and automatic calculation
'Turn off Display Alerts
Application.DisplayAlerts = False
'Turn off Screen Update
Application.ScreenUpdating = False
'Turn off Automatic Calculations
Application.Calculation = xlManual
'_______________________________________________________________________________________________________________
'Dimensioning
Dim j As Long
Dim LastRow As Long
Dim StartTime As Double
Dim SecondsElapsed As Double
'_______________________________________________________________________________________________________________
StartTime = Timer 'Remember time when macro starts
Sheets("Sheet1").Activate
LastRow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
'Moving and adjusting columns
'Column C move
Range("C8:C" & LastRow).Cut Range("H8")
'Column D copy and paste values and then move
For j = 9 To LastRow
If Range("D" & j) <> "" Then Range("D" & j).Copy Range("E" & j)
Next j
Range("D8:D" & LastRow).Clear
'Determine how many seconds code took to run
SecondsElapsed = Round(Timer - StartTime, 2)
'Notify user in seconds
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
'_________________________________________________________________________________________________________________
'Turn on alerts, screen updates, and calculate
'Turn On Display Alerts
'Application.DisplayAlerts = True
'Turn on Screen Update
'Application.ScreenUpdating = True
'Turn off Automatic Calculations
'Calculate
'_________________________________________________________________________________________________________________
'Place the curser in cell
End Sub