Dan M
Dan M

Reputation: 35

Simple SumIf in vba

I'm trying to do a sumifs function in vba using a prompted range. Here's what I have so far:

Sub SumIfPositiveSelectedRange()
    Dim ThisRng As Range
    Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
    ActiveCell.Formula = "=SUMIF((" & ThisRng.Address & "),">0")"
    End Sub

I get a Complile Error: Expected: end of statement. What am I missing?

Upvotes: 1

Views: 246

Answers (1)

Scott Craner
Scott Craner

Reputation: 152450

As per my Comments. When wanting the quotes to remain in the string one must use double quotes like thus:

Sub SumSelectedRange()
Dim ThisRng As Range
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
ActiveCell.Formula = "=SUMif(" & ThisRng.Address & ","">0"")"
End Sub

Upvotes: 2

Related Questions