0

I defined two named ranges; one (myplace) for the location to write the result to and one (myrange) for the range that needs to be summed. Compiler returns an error

Tried the formula method for sum, but this returns "#NAAM" error

This is the simple code I used:

Sub optelsom()

myplace = Range("plaats") 
myrange = Range("deelsom")

Range(myplace).Value = Application.WorksheetFunction.Sum(myrange)

End Sub

1 Answer 1

1

(1) Get in the habit of declaring your variables (as per PEH's suggestion, use Option Explicit: in the VBA editor go to Tools › Options › Require Variable Declaration)

(2) You need Set when assigning object variables such as ranges

(3) myplace is already a range so no need for a 'wrapper'

Sub optelsom()

Dim myplace as range, myrange as range

Set myplace = Range("plaats") 
Set myrange = Range("deelsom")

myplace.Value = Application.WorksheetFunction.Sum(myrange)

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

I recommend always to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.