To Start with:
Did you actually take a look at the formula entered into the cell by the code?
SUM(Cells(x+1, 4) : Cells(x+y+1, 4))
Excel simply doesn't know what the "X" and "Y" values are (and see my little soapbox about strings in functions (^_^)).
You need to either have cells/ranges named as "X" and "Y" in the work sheet or re-write you code so that the "X" and "Y" values are evaluated.
Even then you are going to get the #Name error because "Cells" is not a defined function at the worksheet level.
Now, you've hit upon something that just drives me crazy...
building the string within the command - and it's not your fault because that's how a majority of examples show how to use the command.
Instead I suggest that you build the string first and then use the string in the command. Why you might ask, because you can then check how the string is actually resolving; thus, making troubleshooting the code so much easier as most of the time the issue is with something missing or not resolving properly/as expected within your string.
Because what you are trying to do is little obscure (that is to say... I don't know exactly what you are trying to do) I'll take a stab based on what I think your code was trying to accomplish.
- Sub poc1()
-
Dim zx As Long
-
Dim zy As Long
-
Dim zs As String
-
'
-
'now because I don't know your loops etc.. I just picked a few numbers
-
zx = 5
-
zy = 10
-
'
-
'let's just start in the first cell
-
Range("A1").Select
-
'
-
'enter the formula.... the address() returns the string (in this case absolute) to
-
' the referenced cell,
-
zs = "=sum(indirect(address(" & zx & "+1,4)):indirect(address(" & zx + zy & "+1,4)))"
-
'
-
'See the advanatage here... you can add the debug.print to see the resolved string
-
Debug.Print zs
-
'if you don't have it open, press <ctrl><g> now, and the immediate window will open and
-
'you can see the resolved string
-
'
-
'so for zx=5 and zy=10
-
' in cell D19, =sum(indirect(address(5+1,4)):indirect(address(15+1,4)))
-
' which is the equivalent of =sum($D$6:$D$16)
-
' also changed your "D" to 4
-
Cells(zx + zy + 4, 4).Formula = zs
-
End Sub