473,473 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

#NAME? error

7 New Member
Hi I'm getting the worksheet error #NAME? when my formula in VBA gets evaluated.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  Cells(x + y + 4, "D").Formula = "=sum(cells(x+1 , 4) : cells(x+y+1 , 4))"
  3.  
  4.  
I know that the error is refering to the word cells in the formula, but I don't know how to make Excel accept a formula that is not in the usual ColRow : ColRow format.
Nov 23 '13 #1
3 1587
zmbd
5,501 Recognized Expert Moderator Expert
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.
Expand|Select|Wrap|Line Numbers
  1. Sub poc1()
  2.     Dim zx As Long
  3.     Dim zy As Long
  4.     Dim zs As String
  5.     '
  6.     'now because I don't know your loops etc.. I just picked a few numbers
  7.     zx = 5
  8.     zy = 10
  9.     '
  10.     'let's just start in the first cell
  11.     Range("A1").Select
  12.     '
  13.     'enter the formula.... the address() returns the string (in this case absolute) to
  14.     ' the referenced cell,
  15.     zs = "=sum(indirect(address(" & zx & "+1,4)):indirect(address(" & zx + zy & "+1,4)))"
  16.     '
  17.     'See the advanatage here... you can add the debug.print to see the resolved string
  18.     Debug.Print zs
  19.     'if you don't have it open, press <ctrl><g> now, and the immediate window will open and
  20.     'you can see the resolved string
  21.     '
  22.     'so for zx=5 and zy=10
  23.     ' in cell D19, =sum(indirect(address(5+1,4)):indirect(address(15+1,4)))
  24.     ' which is the equivalent of =sum($D$6:$D$16)
  25.     ' also changed your "D" to 4
  26.     Cells(zx + zy + 4, 4).Formula = zs
  27. End Sub
Nov 26 '13 #2
tryinghard
7 New Member
Hi
Thanks for your input.
I obviously didn't include the reams of code that define the values of x & y that would have just confused things.

I now know what I missed in that piece of Formula code, namely the use of R1C1 and " & x & ".

I prefer to use the column as a letter "D" instead of the column number 4. It makes understanding the code later on a tad easier.

I take your point that building the string first does have its advantages, it does however add more lines and hence more bulk and more code to slog through, but better is always preferable.

Thanks again for your input.
Nov 26 '13 #3
zmbd
5,501 Recognized Expert Moderator Expert
The extra line of code is really a very trival thing for today's PCs... you could put a few hundred lines in there like the one we're using and the PC wouldn't even blink an eyee.

As for the use of "D" or the numeric is up to you; however, it is USUALLY much better to maintain a consistaint usage. Switching back and forth like that will only confuse things later on... especially for anyone that follows you as the expectation is to use one method of reference.

You can not use the R1C1 and A1 notation at the sametime at the worksheet level. Also, R1C1 notation is falling out of favor for all but legacy compatability and for use in VBA coding.

best of luck
-z
Nov 26 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: hawk | last post by:
I am creating a series of custom reports, whose parameters are specified through combo boxes in a form, which then generates a custom report based on a series of calculation queries. My problem is...
1
by: Yuki | last post by:
Thanks for taking the time to look at my problem. I keep getting a #Name error from a calculated field on a form. It is a text box field unbound. The calculation is very very simple yet I still...
3
by: Regnab | last post by:
I'm not sure if this is some random bug in the system, but I keep getting the #Name? error on one of my forms when I try to link one field to another. For example, I have one field called...
3
by: beat1078 | last post by:
I have a report that I create using unbound text boxes to link to an open form. When you click on a cmdbutton on the form the report is supposed to print out information from the form. It works...
5
by: bitsnbytes64 | last post by:
Hi, I have a test form with: * textbox txtIXO_NR bound to column IXO_NR through the linked SQL Server 2005 table TXOCTC (link name "dbo_TXOCTC") * an unbound textbox which should display the...
8
by: teneesh | last post by:
I am trying to get rid of the #Name error that appears in my txtHosp text box when I run this code. the code below is also a call command, so any time I am updating a combo box, the following error...
1
by: Paulson | last post by:
Hi all I got a problem in my PHP code while uploading huge files The error is as follows __________________________________________________________________ $_FILE -...
4
by: jkriner | last post by:
#Name? error message in Access Form -------------------------------------------------------------------------------- Hello I have created a simple database with 4 tables (all the same fields in...
3
by: shayvillere | last post by:
I'm designing a report and cannot pull data from a query. The control gives me a #Name? error when I do a print preview. I never had this problem with Access 2000. Any ideas?
6
by: codicecrm | last post by:
am trying to create a main report containing several subreports in Access 2003, all based on individual queries. The data for each subreport actually displays just fine, but I am having problems...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.