Hi,
Sorry, I was in a hurry this morning when I sent you the code.
I forgot to say that you need to click on the button to run the macro.
I used the names in rows 23 to 32 to test and compare the results with the names in rows 10-19. You can change the loop and delete those names I added.
Here's an updated version of the code, that will loop through all sheets. Change the values of shNames as required.
Sub Compile()
Dim Dic, DicT
Dim ar, shNames
Dim i As Long, n As Long, j As Long, sName As String
Dim rg As Range, c As Range
Dim ws As Worksheet
Application.ScreenUpdating = False
shNames = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC" '<<SHEET NAMES
shNames = Split(shNames, ",")
Set Dic = CreateObject("scripting.dictionary")
'Loop through all sheets
For n = LBound(shNames) To UBound(shNames)
'Check if sheets exists
On Error Resume Next
Set ws = Sheets(shNames(n))
On Error GoTo 0
If Not ws Is Nothing Then
ar = ws.Cells(1).CurrentRegion.Value
'Populate dictionary
For i = 1 To UBound(ar, 1)
If ar(i, 1) <> "" Then
If Not Dic.exists(ar(i, 1)) Then
Set Dic(ar(i, 1)) = CreateObject("Scripting.dictionary")
Dic(ar(i, 1)).Add ar(i, 7), ar(i, 10)
Else
Dic(ar(i, 1)).Add ar(i, 7), ar(i, 10)
End If
End If
Next i
Set ws = Nothing
End If
Next n
'Write back in sheet
With Sheets("EMER")
For n = 23 To 32 '<<TEST ONLY, CHANGE THE LOOP
sName = .Cells(n, 2)
If Dic.exists(sName) Then
Set DicT = Dic(sName)
Set rg = .Range("Q1:CA1") '<<CHANGE THIS RANGE TO HAVE COMPLETE YEAR
For Each c In rg
If DicT.exists(c.Value) Then .Cells(n, c.Column) = DicT(c.Value)
Next c
End If
Next n
End With
Application.ScreenUpdating = True
End Sub
Bookmarks