I am trying to write a macro in VBA for excel that allows a user to input a range of cells. Then, I want the macro to sum up all of the cells, and input them into cell R32.
I have attached the code that I wrote for it so far. When I run the macro, I am allowed to select my range of cells.
However, I then receive the error message, run time error 1004- method range of object _Worksheet failed. This seems like something that could be a quick fix... But I am very stuck. Any help would be greatly appreciated.
Private Sub CommandButton5_Click()
Dim userResponse As Range
On Error Resume Next
Set userResponse = Application.InputBox("select a range with the mouse",Default:=Selection.Address, Type:=8)
On Error GoTo 0
If userResponse Is Nothing Then
MsgBox "Cancel clicked"
Else
MsgBox "You selected " & userResponse.Address
End If
Range("R32").Value = Application.WorksheetFunction.Sum(Range(userResponse))
End Sub
Range("R32").Value = Application.WorksheetFunction.Sum(Range(userResponse))will give you an error whenuserResponseis nothing. You need to put it inside the if statement testing for nothingness.