1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
Sub Color_SFG(plants)
Dim nbPlants As Integer, nbSFG As Integer, Lastline As Integer, LastRow As Integer
Dim PArray() As Variant
Dim cell As Range
Dim i As Integer, j As Integer, x As Integer
Dim isSFG As Boolean
Dim RegExp As Object
Dim ws As Worksheet
Dim colMatches As MatchCollection
'Initializing an Instance
Set RegExp = CreateObject("vbscript.regexp")
'Reg exp Setttings
RegExp.Global = True
RegExp.Pattern = "[BOM]{3}\d{4}"
'String length (x) with BOM & - & 4 digits(0-9) with the 1st = 1
RegExp.IgnoreCase = False
Application.ScreenUpdating = False
Worksheets("PDP Data").Activate
Lastline = Range("A5").End(xlDown).Row
nbPlants = UBound(plants)
ReDim PArray(nbPlants, 1)
For Each ws In Application.ActiveWorkbook.Sheets
'Test whether the String can be compared.
If RegExp.Test(ws.Name) = True Then
'Get the matches.
'Execute search.
Set colMatches = RegExp.Execute(CStr(ws.Name))
For i = LBound(plants) To UBound(plants)
If Right(CStr(colMatches(0)), 4) = plants(i) Then
PArray(i, 1) = plants(i)
Worksheets(CStr(colMatches(0))).Activate
Do
j = j + 1
While Range("A" & j) <> colMatches(0)
LastRow = Range("A" & j).End(xlDown).Row - 1
Range("A" & j + 1 & ":A" & LastRow).Select
nbSFG = Distinct_count(Selection)
If (nbSFG + 1 > UBound(PArray, 2)) Then
ReDim Preserve PArray(1 To nbPlants, 1 To nbSFG + 1)
End If
isSFG = False
For Each cell In Worksheets(CStr(colMatches(0))).UsedRange.Rows
If isSFG = True And IsNumeric(Range("A" & cell.Row)) = True Then
For j = LBound(PArray, 2) + 1 To UBound(PArray, 2)
If IsEmpty(PArray(i, j)) Then
PArray(i, j) = Range("A" & cell.Row)
Exit For
End If
If Range("A" & cell.Row) = PArray(i, j) Then GoTo nextcell
Next j
ElseIf isSFG = False Then
If Cells(cell.Row, 1) = "SFG Code" Then isSFG = True
End If
nextcell:
Next cell
End If
Next i
End If
Next ws
End Sub |
Partager