See what i read your question
Sub test()
Dim shtTemp As Worksheet, shtDB As Worksheet
Dim r As Range, s As String, i As Long, MyVal As Long
Dim myRange As Range, MyYear As Range, myMonth As Range
Dim sCompany As String, sYear As String, sMonth As String
Set shtTemp = Worksheets("Template")
Set shtDB = Worksheets("DB")
Set myRange = shtDB.Range("A1:A" & shtDB.Range("A" & Rows.Count).End(xlUp).Row)
Set myMonth = shtDB.Range("C1:C" & shtDB.Range("A" & Rows.Count).End(xlUp).Row)
Set MyYear = shtDB.Range("B1:B" & shtDB.Range("A" & Rows.Count).End(xlUp).Row)
MyVal = shtTemp.Range("B21").Value
sCompany = shtTemp.Range("A5").Value
sYear = shtTemp.Range("B5").Value
If WorksheetFunction.SumIfs(shtDB.Range("D1:D" & shtDB.Range("A" & Rows.Count).End(xlUp).Row), myRange, sCompany, MyYear, sYear) = MyVal And _
WorksheetFunction.CountIf(myRange, sCompany) > 0 Then
MsgBox "No Changes done", vbInformation
GoTo EndProcess
ElseIf WorksheetFunction.SumIfs(shtDB.Range("D1:D" & shtDB.Range("A" & Rows.Count).End(xlUp).Row), myRange, sCompany, MyYear, sYear) <> 0 And _
WorksheetFunction.CountIf(myRange, sCompany) <> 0 Then
For i = 1 To 12
sMonth = shtTemp.Range("A" & i + 7).Value
Set r = myRange.Find(sCompany)
If Not r Is Nothing Then
Do While Not (r.Offset(, 1).Value = sYear And r.Offset(, 2).Value = sMonth)
Set r = r.Offset(1, 0)
If Not r Is Nothing Then
r.Select
End If
Loop
r.Offset(, 3).Value = shtTemp.Range("B" & i + 7).Value
End If
Next i
Else
For i = 1 To 12
sMonth = shtTemp.Range("A" & i + 7).Value
shtDB.Range("A" & Rows.Count).End(xlUp).Offset(1).Value = sCompany
shtDB.Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = sYear
shtDB.Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = sMonth
shtDB.Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = shtTemp.Range("B" & i + 7).Value
Next i
End If
MsgBox "Add/Updates Successfully", vbInformation
EndProcess:
End Sub
Bookmarks