mercredi 4 janvier 2017

Excel Macro If Else issue

I am having issues getting my If else statment to work, as below.

I want to sum everything from G5 down, but if G6 is blank (ie there is only one entry), then i just want to sum G5. So i have come up with the code below.

However, i am getting an error saying Else without If. But, if i put If after the else, i get another syntax error.

Clearly i am missing something, but cant figure out what is wrong.

Sub TotalR()

Sheets("Data").Select

Dim Rng As Range
Dim c As Range

If IsEmpty(Range("g6")) = True Then Range("g6").Select
ActiveCell.Formula = "=sum(g5)"
Else:
    Set Rng = Range("g5:g" & Range("g5").End(xlDown).Row)
    Set c = Range("g5").End(xlDown).Offset(1, 0)
    c.Formula = "=SUM(" & Rng.Address(False, False) & ")"

End If

End Sub

Aucun commentaire:

Enregistrer un commentaire