OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for any suggestions for which feedback will be given.
This code runs through a series of tabs and wherever it sees an error, it takes that line and places it in the "Error.Items" sheet. Why is it duplicating each error? I guess I could write code at the end to remove the duplicate lines, but I would like to figure out what I am doing wrong. Also, some of the code I don't think I need so feel free to suggest. For example, is the following needed?
The following is the entire code:
This code runs through a series of tabs and wherever it sees an error, it takes that line and places it in the "Error.Items" sheet. Why is it duplicating each error? I guess I could write code at the end to remove the duplicate lines, but I would like to figure out what I am doing wrong. Also, some of the code I don't think I need so feel free to suggest. For example, is the following needed?
Code:
If J = 1 Or J = 2 Or J = 6 Or J = 7 _
Or J = 8 Or J = 9 Or J = 10 Or J = 11 Or J = 12 _
Or J = 9 Or J = 10 Or J = 11 Or J = 12 Or J = 14 _
Or J = 15 Or J = 17 Or J = 19 Or J = 20 Or J = 21 _
Or J = 22 Or J = 23 Or J = 24 Or J = 27 _
Or J = 30 Or J = 31 Or J = 32 Or J = 33 Or J = 34 _
Or J = 35 Or J = 36 Or J = 37 Or J = 38 Or J = 39 _
Or J = 40 Then
'do nothing
The following is the entire code:
Code:
Sub Error_Check()
'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 LastRow As Long
Dim Error_Row As Long
Dim Error_Count As Long
Dim Error_Count_Clm As Long
Dim i As Long, J As Long, k As Long
Dim X As Long
'Looping through all the tabs to find the errors
Error_Row = 3
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "Error.Items" Or Sheet.Name = "JIBs.End" Then
For X = Sheets("JIBs.End").Index - 1 To Sheets("Error.Items").Index + 1 Step -1
'Find the last row of data
Sheets(X).Activate
LastRow = Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row
Error_Count = 0
For i = 3 To LastRow
Error_Count = 0
For J = 1 To 41
If J = 1 Or J = 2 Or J = 6 Or J = 7 _
Or J = 8 Or J = 9 Or J = 10 Or J = 11 Or J = 12 _
Or J = 9 Or J = 10 Or J = 11 Or J = 12 Or J = 14 _
Or J = 15 Or J = 17 Or J = 19 Or J = 20 Or J = 21 _
Or J = 22 Or J = 23 Or J = 24 Or J = 27 _
Or J = 30 Or J = 31 Or J = 32 Or J = 33 Or J = 34 _
Or J = 35 Or J = 36 Or J = 37 Or J = 38 Or J = 39 _
Or J = 40 Then
'do nothing
ElseIf Cells(i, J) = "" And J = 3 Then
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
ElseIf Cells(i, J) = "No CC Xref" And J = 5 Then
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
ElseIf J = 5 Then
If Cells(i, J) = "" Or Cells(i, J) = "No CC Xref" Then
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
End If
ElseIf J = 13 Then
If Cells(i, J) <> 1 And Cells(i, J) <> 3 Then
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
End If
ElseIf Cells(i, J) = "" And J = 16 Then
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
ElseIf Cells(i, J) = "" And J = 18 Then
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
ElseIf Cells(i, J) = "" And J = 26 Then
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
ElseIf Cells(i, J) = "!Xref Error" And J = 28 Then
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
ElseIf Cells(i, J) = "" And J = 29 And Cells(i, 25) <> "REVENUE" Then
Cells(i, 25).Font.Bold = True
Cells(i, 25).Interior.ColorIndex = 2
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
ElseIf Cells(i, J) = "" And J = 41 Then
Cells(i, J).Font.Bold = True
Cells(i, J).Interior.ColorIndex = 3
End If
'Count the number of errors in each row
If Cells(i, J).Font.Bold = True Then
Error_Count = Error_Count + 1
End If
Next J
'Designate the errors
If Error_Count > 1 Then
Range("AS" & i).Value = "Yes"
Range("AS" & i).Font.Bold = True
Range("AS" & i).Interior.ColorIndex = 3
Range("AX" & i).Value = Error_Count
'Indicate which errors are present
Error_Count_Clm = Error_Count
'Partner CC
If Range("E" & i).Font.Bold = True Then
Range("AT" & i).Value = "Yes"
Range("AT" & i).Font.Bold = True
Error_Count_Clm = Error_Count_Clm - 1
Range("AY" & i).Value = Error_Count_Clm
Else
Range("AT" & i).Value = "No"
Range("AY" & i).Value = Error_Count_Clm
End If
'Prt Actt
If Range("AB" & i).Font.Bold = True Then
Range("AU" & i).Value = "Yes"
Range("AU" & i).Font.Bold = True
Error_Count_Clm = Error_Count_Clm - 1
Range("AZ" & i).Value = Error_Count_Clm
Else
Range("AU" & i).Value = "No"
Range("AZ" & i).Value = Error_Count_Clm
End If
'Other Errors
If Error_Count_Clm > 0 Then
Range("AV" & i).Value = "Yes"
Range("AV" & i).Font.Bold = True
Error_Count_Clm = Error_Count_Clm - 1
Range("BA" & i).Value = Error_Count_Clm
Else
Range("AV" & i).Value = "No"
Range("BA" & i).Value = Error_Count_Clm
End If
Range("A" & i & ":BA" & i).Copy Sheets("Error.Items").Range("C" & Error_Row)
Sheets("Error.Items").Range("A" & Error_Row).Value = ActiveSheet.Name
Sheets("Error.Items").Range("B" & Error_Row).Value = i
Error_Row = Error_Row + 1
End If
Next i
Next
End If
Next
'Sort the data
'Find the last row first
Sheets("Error.Items").Activate
LastRow = Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row
'Sorting the Tab Names in Order
With Sheets("Error.Items").Sort
.SortFields.Add Key:=Range("A3"), Order:=xlAscending
.SortFields.Add Key:=Range("B3"), Order:=xlAscending
.SetRange Range("A3:AV" & LastRow)
.Header = xlNo
.Apply
End With
'Formatting the "Error.Items" tab to autofit
Sheets("Error.Items").Columns("A:AX").EntireColumn.AutoFit
'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
'Freeze Panes on the "Error.Items" Tab and places the cursor in cell C3
Sheets("Error.Items").Activate
Sheets("Error.Items").Range("C3").Select
ActiveWindow.FreezePanes = False
ActiveWindow.FreezePanes = True
End Sub