I've been writing some code that places some data in an empty worksheet and then formats the sheet by adding borders to all of the used cells. The code functions how it should perfect, but it takes a little bit of time to execute. I was wondering if there is any way I can speed up my code that I'm missing, because I don't know if there is a more efficient way to do what I've done.
Sub Report()
Application.ScreenUpdating = False
Sheets("Raw Data").Select
Application.Calculation = xlCalculationManual
Sheets("Raw Data").Unprotect
Req = UniqueRequest()
SheetForm = ReqSheetFormat()
ReqColor = ReqColorCount()
Sheets("Raw Data").Protect
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Function ColSearch(Heading As String) As Integer
'Determines the column number of the desired heading
Sheets("Raw Data").Select
Sheets("Raw Data").Unprotect
Dim myCol As Integer
'Determines column number for desired string
myCol = Sheets("Raw Data").Cells.Find(What:=Heading, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Column
ColSearch = myCol
End Function
Function UniqueRequest() As Long
'Creates dictionary full of unique request ID's and lists them in the "Request Results" sheet
Dim d As Object, tmp2
Dim LastRow As Long
Dim LastColumn As Long
Dim i As Long
Dim n As Long
Dim x As Long
Dim cllsct As Long
Dim myReqIDCol As Integer
Dim myNameCol As Integer
Dim myTypeCol As Integer
Dim myDescCol As Integer
Dim myVerCol As Integer
Dim myTstCol As Integer
'Determines the last row and the last column of the table
LastRow = Range("A" & Rows.count).End(xlUp).row
LastColumn = Cells(1, Columns.count).End(xlToLeft).Column
'Determines desired column number and stores it to a variable
myReqIDCol = ColSearch("id")
myNameCol = ColSearch("ownerFullname")
myTypeCol = ColSearch("type")
myDescCol = ColSearch("description")
myVerCol = ColSearch("State")
myTstCol = ColSearch("testResults")
n = 1
'Creates a dictionary filled with each unique value in the "id" column and stores the unique ID's to the "Request Results" sheet
Set d = CreateObject("Scripting.Dictionary")
For i = 2 To LastRow
tmp2 = Cells(i, myReqIDCol).Value
If tmp2 <> Empty Then
If Not d.exists(tmp2) Then
d.Add tmp2, 1
n = n + 1
cllsct = Cells(i, myReqIDCol).Select
x = ActiveCell.row
NeedRev = DRev(x, n, myTypeCol, myDescCol, myTstCol, myVerCol)
End If
End If
FullName = SelectName(x, n, myNameCol, myTypeCol)
Next i
'Pastes ID's in the "Request Results" sheet
ThisWorkbook.Worksheets("Request Results").Range("A2").Resize(UBound(d.keys, 1) + 1).Value = Application.Transpose(d.keys)
End Function
Function ReqSheetFormat() As Long
'Formats "Request Results" sheet
Dim i As Long
Dim j As Long
Dim LastRow As Long
Dim LastColumn As Long
'Creates header row for "Request Results" sheet
With Sheets("Request Results")
.Cells(1, 1).Value = "Request ID"
.Cells(1, 2).Value = "Owner Name"
.Cells(1, 3).Value = "Request Type"
.Cells(1, 4).Value = "State"
.Cells(1, 5).Value = "Design Review Required"
.Cells(1, 6).Value = "Design"
.Cells(1, 7).Value = "Design Review"
.Cells(1, 8).Value = "Test Results"
End With
'Determines the last row and the last column of the table
Sheets("Request Results").Select
LastColumn = Cells(1, Columns.count).End(xlToLeft).Column
LastRow = Range("A" & Rows.count).End(xlUp).row
'Locks first row of sheet
Rows("2:2").Select
ActiveWindow.FreezePanes = True
'Colors cells and formats sheet
For i = 1 To LastRow
If i >= 2 Then
If Worksheets("Request Results").Cells(i, 5).Value = "Yes" And Worksheets("Request Results").Cells(i, 7).Value = "No" And Worksheets("Request Results").Cells(i, 7).Interior.Color <> RGB(255, 255, 0) Then
Worksheets("Request Results").Cells(i, 7).Interior.Color = 255
ElseIf Worksheets("Request Results").Cells(i, 7).Value = "Yes" Then
Worksheets("Request Results").Cells(i, 7).Interior.Color = RGB(0, 176, 80)
ElseIf Worksheets("Request Results").Cells(i, 7).Value = "No" And Worksheets("Request Results").Cells(i, 7).Interior.Color <> RGB(255, 255, 0) Then
Worksheets("Request Results").Cells(i, 7).Interior.Color = RGB(0, 176, 80)
End If
If Worksheets("Request Results").Cells(i, 4).Value = "Rejected" Then
Worksheets("Request Results").Cells(i, 5).Value = "N/A"
Worksheets("Request Results").Cells(i, 6).Value = "N/A"
Worksheets("Request Results").Cells(i, 7).Value = "N/A"
Worksheets("Request Results").Cells(i, 7).Interior.Color = RGB(0, 176, 80)
Worksheets("Request Results").Cells(i, 8).Value = "N/A"
Worksheets("Request Results").Cells(i, 8).Interior.Color = RGB(0, 176, 80)
End If
End If
With Sheets("Request Results").Range(Cells(1, 1), Cells(1, LastColumn))
.Borders(xlEdgeBottom).Color = 0
.Borders(xlEdgeBottom).Weight = xlThick
End With
With Sheets("Request Results").Range(Cells(i, 1), Cells(i, LastColumn))
.Borders(xlEdgeBottom).Color = 0
.Borders(xlEdgeBottom).Weight = xlThin
End With
With Sheets("Request Results").Range(Cells(1, 1), Cells(1, LastColumn))
.Borders(xlEdgeTop).Color = 0
.Borders(xlEdgeTop).Weight = xlThick
End With
With Sheets("Request Results").Range(Cells(1, 1), Cells(LastRow, 1))
.Borders(xlEdgeLeft).Color = 0
.Borders(xlEdgeLeft).Weight = xlThick
End With
With Sheets("Request Results").Range(Cells(LastRow, 1), Cells(LastRow, LastColumn))
.Borders(xlEdgeBottom).Color = 0
.Borders(xlEdgeBottom).Weight = xlThick
End With
With Sheets("Request Results").Range(Cells(1, LastColumn), Cells(LastRow, LastColumn))
.Borders(xlEdgeRight).Color = 0
.Borders(xlEdgeRight).Weight = xlThick
End With
For j = 1 To (LastColumn - 1)
With Sheets("Request Results").Range(Cells(i, 1), Cells(i, j))
.Borders(xlEdgeRight).Color = 0
.Borders(xlEdgeRight).Weight = xlThin
End With
Next j
Next i
'Aligns text of sheet to the left
Sheets("Request Results").Select
Columns("A:Z").HorizontalAlignment = xlLeft
'Adds filter to the first row of sheet
Range(Cells(1, 1), Cells(1, LastColumn)).Select
Selection.AutoFilter
'Disbales text wrapping for better formatting and autofits the cells
Cells.WrapText = False
ActiveSheet.Cells.Columns.AutoFit
End Function
I'm assuming it's the for
loop that that's the cause for the slow execution time, but I'm not quite sure if there is a better way to set it up. Also, I don't have it shown in the code below, but I have Application.ScreenUpdating = False
.