VeryForgetful
Board Regular
- Joined
- Mar 1, 2015
- Messages
- 242
Hi, can anyone recommend a better way of writing the code below please to eliminate the need for multiple loops.
Code:
[COLOR=#000000][FONT="]Sub MaterialThreshold()[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] Dim DataSht As Worksheet[/FONT][/COLOR]
[COLOR=#000000][FONT="] Dim MaterialRange As Range[/FONT][/COLOR]
[COLOR=#000000][FONT="] Dim MaterialCheck As Integer[/FONT][/COLOR]
[COLOR=#000000][FONT="] Dim ErrorCount As Integer[/FONT][/COLOR]
[COLOR=#000000][FONT="] Dim lr As Long[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] Set DataSht = Sheets("Test")[/FONT][/COLOR]
[COLOR=#000000][FONT="] Set MaterialRange = Sheets("Calculations").Range("M2:N15")[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] lr = DataSht.Range("B" & Rows.Count).End(xlUp).Row[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] DataSht.Range("J2:S" & lr).Interior.ColorIndex = xlNone[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] ErrorCount = 0[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] For Each c In DataSht.Range("J2:J" & lr)[/FONT][/COLOR]
[COLOR=#000000][FONT="] On Error Resume Next[/FONT][/COLOR]
[COLOR=#000000][FONT="] MaterialCheck = WorksheetFunction.VLookup(c, MaterialRange, 2, False)[/FONT][/COLOR]
[COLOR=#000000][FONT="] If c.Offset(0, 1).Value > MaterialCheck Then[/FONT][/COLOR]
[COLOR=#000000][FONT="] ErrorCount = ErrorCount + 1[/FONT][/COLOR]
[COLOR=#000000][FONT="] Range(c, c.Offset(0, 1)).Interior.ColorIndex = 3[/FONT][/COLOR]
[COLOR=#000000][FONT="] End If[/FONT][/COLOR]
[COLOR=#000000][FONT="] Next c[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] For Each c In DataSht.Range("L2:L" & lr)[/FONT][/COLOR]
[COLOR=#000000][FONT="] On Error Resume Next[/FONT][/COLOR]
[COLOR=#000000][FONT="] MaterialCheck = WorksheetFunction.VLookup(c, MaterialRange, 2, False)[/FONT][/COLOR]
[COLOR=#000000][FONT="] If c.Offset(0, 1).Value > MaterialCheck Then[/FONT][/COLOR]
[COLOR=#000000][FONT="] ErrorCount = ErrorCount + 1[/FONT][/COLOR]
[COLOR=#000000][FONT="] Range(c, c.Offset(0, 1)).Interior.ColorIndex = 3[/FONT][/COLOR]
[COLOR=#000000][FONT="] End If[/FONT][/COLOR]
[COLOR=#000000][FONT="] Next c[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] For Each c In DataSht.Range("N2:N" & lr)[/FONT][/COLOR]
[COLOR=#000000][FONT="] On Error Resume Next[/FONT][/COLOR]
[COLOR=#000000][FONT="] MaterialCheck = WorksheetFunction.VLookup(c, MaterialRange, 2, False)[/FONT][/COLOR]
[COLOR=#000000][FONT="] If c.Offset(0, 1).Value > MaterialCheck Then[/FONT][/COLOR]
[COLOR=#000000][FONT="] ErrorCount = ErrorCount + 1[/FONT][/COLOR]
[COLOR=#000000][FONT="] Range(c, c.Offset(0, 1)).Interior.ColorIndex = 3[/FONT][/COLOR]
[COLOR=#000000][FONT="] End If[/FONT][/COLOR]
[COLOR=#000000][FONT="] Next c[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] For Each c In DataSht.Range("P2:P" & lr)[/FONT][/COLOR]
[COLOR=#000000][FONT="] On Error Resume Next[/FONT][/COLOR]
[COLOR=#000000][FONT="] MaterialCheck = WorksheetFunction.VLookup(c, MaterialRange, 2, False)[/FONT][/COLOR]
[COLOR=#000000][FONT="] If c.Offset(0, 1).Value > MaterialCheck Then[/FONT][/COLOR]
[COLOR=#000000][FONT="] ErrorCount = ErrorCount + 1[/FONT][/COLOR]
[COLOR=#000000][FONT="] Range(c, c.Offset(0, 1)).Interior.ColorIndex = 3[/FONT][/COLOR]
[COLOR=#000000][FONT="] End If[/FONT][/COLOR]
[COLOR=#000000][FONT="] Next c[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] For Each c In DataSht.Range("R2:R" & lr)[/FONT][/COLOR]
[COLOR=#000000][FONT="] On Error Resume Next[/FONT][/COLOR]
[COLOR=#000000][FONT="] MaterialCheck = WorksheetFunction.VLookup(c, MaterialRange, 2, False)[/FONT][/COLOR]
[COLOR=#000000][FONT="] If c.Offset(0, 1).Value > MaterialCheck Then[/FONT][/COLOR]
[COLOR=#000000][FONT="] ErrorCount = ErrorCount + 1[/FONT][/COLOR]
[COLOR=#000000][FONT="] Range(c, c.Offset(0, 1)).Interior.ColorIndex = 3[/FONT][/COLOR]
[COLOR=#000000][FONT="] End If[/FONT][/COLOR]
[COLOR=#000000][FONT="] Next c[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="] If ErrorCount > 0 Then MsgBox "There are " & ErrorCount & " jobs highlighted in red with potential errors." & _[/FONT][/COLOR]
[COLOR=#000000][FONT="] vbNewLine & vbNewLine & "Please check before sending", vbExclamation[/FONT][/COLOR]
[COLOR=#000000][FONT="]
[/FONT][/COLOR]
[COLOR=#000000][FONT="]End Sub[/FONT][/COLOR]