Dear All,
I am new VBA user and I would like your help because I am getting crazy. I am using a data set with 2 columns. In the first one I have the years (1970-2013) and in the second column the month of an event. What I am trying to do is to manipulate these two columns and create a third one collecting the number of these events in a quarterly form. For the quarters that no event has been occurred I just put a 0 for them.
The problem is that my code is working fine until it meets a missing year. There is something wrong and I CANT SEE IT. Can anyone help me with it please?? I have attached my data set. Much appreciated.
Sub Number_of_Crisis()
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Dat = 1970
g = 1
For p = 1 To lastRow
If Cells(p, 1) = Dat Then
Cells(g, 4).Name = "Quarter1"
Cells(g + 1, 4).Name = "Quarter2"
Cells(g + 2, 4).Name = "Quarter3"
Cells(g + 3, 4).Name = "Quarter4"
If Cells(p, 2) <= 3 Then
Range("Quarter1").Value = Range("Quarter1").Value + 1
ElseIf Cells(p, 2) > 3 And Cells(p, 2) < 7 Then
Range("Quarter2").Value = Range("Quarter2").Value + 1
ElseIf Cells(p, 2) > 6 And Cells(p, 2) < 10 Then
Range("Quarter3").Value = Range("Quarter3").Value + 1
ElseIf Cells(p, 2) > 9 And Cells(p, 2) < 13 Then
Range("Quarter4").Value = Range("Quarter4").Value + 1
End If
ElseIf Cells(p, 1) = Dat + 1 Then
Dat = Dat + 1
g = g + 4
Cells(g, 4).Name = "Quarter1"
Cells(g + 1, 4).Name = "Quarter2"
Cells(g + 2, 4).Name = "Quarter3"
Cells(g + 3, 4).Name = "Quarter4"
If Cells(p, 2) <= 3 Then
Range("Quarter1").Value = Range("Quarter1").Value + 1
ElseIf Cells(p, 2) > 3 And Cells(p, 2) < 7 Then
Range("Quarter2").Value = Range("Quarter2").Value + 1
ElseIf Cells(p, 2) > 6 And Cells(p, 2) < 10 Then
Range("Quarter3").Value = Range("Quarter3").Value + 1
ElseIf Cells(p, 2) > 9 And Cells(p, 2) < 13 Then
Range("Quarter4").Value = Range("Quarter4").Value + 1
End If
ElseIf Cells(p, 1) <> Dat Then
Dat = Dat + 1
g = g + 4
Cells(g, 4).Name = "Quarter1"
Cells(g + 1, 4).Name = "Quarter2"
Cells(g + 2, 4).Name = "Quarter3"
Cells(g + 3, 4).Name = "Quarter4"
End If
Next p
telas = (Dat - 1970 + 1) * 4
For tel = 1 To telas
If Cells(tel, 4) = "" Then
Cells(tel, 4) = 0
End If
Next tel
End Sub
Bookmarks