0
votes

I have a cell that I would like to place an If function. The if function will display a 1 if the average of a selected range of cells is >=2, else 0. I would like to make a vba that will ask you to select the group of range and then place the formula onto the cell, not the value. That way if the average of the selected group of cells gets changed, the updated cell gets changed as well. I've tried doing this but it ends up with a #value error. Any help is appreciated

Sub update()
    Dim siteRange As Range
    ActiveCell.Select
    If MsgBox("Would you like to update?", vbYesNo, "Update") = vbYes Then
        Set siteRange = Application.InputBox("Select a range with the " & _
            "appropraite sites.", "Sites", Type:=8)
        ActiveCell.Value = "=If(Average(siteRange)>=2,1,0)"
    Else
    End If
End Sub

enter image description here

1
Excel knows nothing of the siteRange VBA variable, that's why.Mathieu Guindon

1 Answers

2
votes

siteRange is a range object but you are treating it as a defined name. Concatenate the range object's address into the formula.

ActiveCell.Formula = "=If(Average(" & siteRange.address & ")>=2, 1, 0)"