Sub FindAndSolveDupes()
Dim CurSheet As String
Dim CurWb As Workbook
Dim DupeColor As String
Dim TableRange As Range, TableCell As Range
Dim Lowest As Long, Highest As Long, ExcludeSheet As String, ExcludeAddress As String, Exclude As Range, TextFormat As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
START:
DupeColor = Sheets("PARAMETERS").Range("M45").DisplayFormat.Interior.ColorIndex
'Default Orange color from Excel Colorindex = 44
Lowest = Sheets("PARAMETERS").Range("M46").Value
Highest = Sheets("PARAMETERS").Range("M47").Value
ExcludeSheet = Sheets("PARAMETERS").Range("M48").Value
ExcludeAddress = Sheets("PARAMETERS").Range("M49").Value
Set Exclude = Sheets(ExcludeSheet).Range(ExcludeAddress)
TextFormat = Sheets("PARAMETERS").Range("M50").Value
Set TableRange = Selection
For Each TableCell In TableRange
If TableCell.DisplayFormat.Interior.ColorIndex = DupeColor Then
TableCell.NumberFormat = "General"
TableCell.Formula = "=Text(RandBetweenInt(" & Lowest & ", " & Highest & ", " & Exclude & "), " & TextFormat & ")"
Application.Calculate
TableCell.Formula = TableCell.Value
End If
Next TableCell
EINDE:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Function RandBetweenInt(Lowest As Long, Highest As Long, Exclude As Range) As Long
Dim R As Long
Dim C As Range
Do
R = Lowest + Int(Rnd() * (Highest + 1 - Lowest))
For Each C In Exclude
If R = C Then Exit For
Next C
Loop Until C Is Nothing
RandBetweenInt = R
Application.Volatile
End Function