+ Reply to Thread
Results 1 to 12 of 12

Using Names for Cells rather than "A1" and benifits for math calculations in a macro

  1. #1
    Registered User
    Join Date
    11-29-2008
    Location
    Houston
    MS-Off Ver
    Office 2003, and 2007
    Posts
    77

    Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    I'm working on a spreadsheet to calculate pressure buildup in a room as a result of introducing a chemical agent in the room (no...it's not a lethal chemical).

    At the moment I'm setting up a userform for the data entry and haven't gotten to coding the calculations yet. In this process I'm seeing where I might need to move some information around in my spreadsheet so that the calculation process is more ordered (i.e. data for step 1 in row 1, step 2 in row 2,.....step 10 in row 10, etc.). Currently the data for step 7 may be in row 3 and so on.

    If I start moving the data around now I'm going to have to tweak a number of macro's which currently are in the form of "a_Un = ['Calculations'!V5].Value: ['Calculations'!BY5].Value = a_Un". While there there is quite a lot of tweaking that would be needed even at this stage, it's going to be a nightmare later.

    Question:
    If I assign names to various cells and referenced those names in the macro's, I'm guessing I could move the cell around all day long and not have to tweak the macro?

    While I don't like using "Names" in formula's because I've always felt it difficult to go back later and modify or fix something. I've read a few posts here that disagree with this, but then again I'm going to be the one that has to maintain it so...

    I've assigned standard engineering nomenclature, using Excel equation editor (Agent Quantity = Aq). While none of the notations at this moment use characters that couldn't be noted with standard SUPERSCRIPT, or SUBSCRIPT formatting, but I'm concerned I have have to (mass for example m"), so I'm sticking with the equation editor for now.

    Since I'll be assigning variables to the values, I'll be using the cell notations (without the script).

    Question:
    Am I correct in assuming there's going to be a clash with naming a cell "Aq" and then assigning a variable with the same name to reference that same cell?
    If so, would a cell name of "Aq", with a variable name of "a_Aq" be an issue?
    Will using cell names in the VBA Code slow things down?
    Will doing the math in the VB code be slower then doing it with a formula?
    Regards,
    Dan
    Real world knowledge isn't dropped from a parachute in the sky but rather acquired in tiny increments from a variety of sources including panic and curiosity.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    13,020

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    I don't know was you mean by "...move a cell around..."
    Say you name cell N6 as NetWeight. In the Name Manager you will see in refers to: =Sheet1!$N$6
    If you drag the cell to another location, the refers to value will change to its new location.

    To reference the cell N6 in VB code you would use such as
    MyVar = Range("NetWeight").Value

    If you declare a variable:
    Dim NetWeight As Double, that variable name is independent of the named range:

    Given: cell N6 holds 22606
    Please Login or Register  to view this content.
    This code will report:
    22606
    11066
    So, Named Ranges and Variable names are independent but could be confusing to read.
    Ben Van Johnson

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,531

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    Just to add to what protonLeah has said, I normally prefix named ranges with "nr" and variables with their type. So, in the example, I would refer to dNetWeight and Range("nrNetWeight").

    I also tend to hide my named ranges so they are not obvious and less likely to get deleted.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    11-29-2008
    Location
    Houston
    MS-Off Ver
    Office 2003, and 2007
    Posts
    77

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    Bob,
    I don't know was you mean by "...move a cell around..."
    If I use my mouse on the spreadsheet to move cell $N$7 which has a name of "NetWeight" to cell $N$24, the name manager automatically compensates for the change (Netweight=$N$7, =$N$24)
    Q1: If I use the cells "Name" in the VBA code (rather than $N$7, then I don't think I have to worry about updating the VBA if I move the cell from N7 to N24...Right?
    Q2: Or...maybe because I didn't use the "$" sign in the VBA, I had to update the code when i move N7 to N24?

    Let's assume for a minute that cell N6 (NetWeigh) has a value of 11066
    This value was established by a "TextBox" on a userform with a name of "NetWeight" (value converted to an integer)

    Cell N7 is named "CylQty", and has a value of "2" (total quantity of cylinders, each with the same netweight).
    The value was established by a "TextBox" on the same userform with a name of "CylQty"

    Cell N8 is named "TotNtWt" (Total Net Weight) and needs to hold the total net weight.
    A variable would be associated with N8 of "TotNtWt" for consistence and possible use elsewhere in the code.
    Q3: Is using the same name in the "Name Manager" as in a VBA Variable good, or poor code practice?
    I like TMS's idea...
    I normally prefix named ranges with "nr" and variables with their type

    On paper the equation would look like NetWeight x CylQty = TotNtWt.
    This math is about 1/10th as complex as most of the equations so most of the formulas would be much longer and harder to decipher.
    Q4: Would using variables to perform calculations in the VBA yield shorter equations that might be simpler to follow?
    Q5: Would using variables allow for faster, or slower execution, or would it matter?
    Last edited by DM2; 03-05-2015 at 06:15 PM.

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    13,020

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    A1: True
    A2: If you use like Range("N6") or Range("$N$6") in code, then you must find and edit every occurance of it to the new cell address.
    A3: TMS's practice is better.
    A4: Yes. Also, if you use names for the ranges and accidentally mis-spell one, the debugger will let you know as soon as you press enter(if you have "OPTION EXPLICIT" enabled). If you are using Range(N6).Value instead of the name and accidentally type M6 or N7 you won't know until you get bad answers or a crash, then you will have to hunt for the mistake.
    A5: I don't know if it will affect execution speed.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,531

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    Q1: Right
    Q2: Dollar signs in the VBA range reference are irrelevant; the range reference will not auto-adjust, they are fixed in your code whatever
    Q3: it is personal choice. In the same way as it is good practice to use a type prefix, it helps to identify your named ranges and, indeed, dynamic named ranges
    Q4: depends on how long you make the variable names. But it is likely to be more readable (and maintainable) if the variable names relate to their content.
    Q5: not sure. Suspect faster if you set the variable once and use it many times; hence you don't have to evaluate range addresses.

    Regards, TMS

  7. #7
    Registered User
    Join Date
    11-29-2008
    Location
    Houston
    MS-Off Ver
    Office 2003, and 2007
    Posts
    77

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    B. Johnson / TMS,
    Thanks for your replies.
    A1: Thought so.
    A2: I've discovered this, and updating is a pain.
    A3: I'm going to follow TMS's lead, or some deviation thereof
    A4: Ahhhh...I like the idea of the error checking as well!!!!!!
    A5: Ease of understanding the code is more important, especially for someone like me...

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,531

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    You're welcome. Thanks for the rep.


    Note: This code
    Please Login or Register  to view this content.
    is more understandable in, say, six moths, than:
    Please Login or Register  to view this content.
    But, clearly, the latter is much shorter

    Regards, TMS

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    I'm pretty fussy about names, and picked up my naming convention partly from Bullen's PED

    Names with Worksheet scope:

    o Fixed single cells start with ptr (e.g., ptrInput)

    o Fixed named ranges start with rgn ("region"), tbl (table), or according to purpose: lut (lookup table), val (validation list)

    o Me refers to the cell in which it appear (RefersToR1C1 "=rc")

    o myRow and myCol ("=r" and "=c") (a common formula I put above or below tables is =sum(tblXxx myCol))

    o Other relative named ranges start with rel (relN/E/S/W are one cell up, right, down, and left "=r[-1]c", "=rc[1]", etc.)

    o Fixed rows start with row (rowHdr) (number rows with a formula like =rows(rowHdr:Me) - 1)

    o Fixed columns start with col (colPrice, colQty)

    o Named formulas start with frm (frmExt "=colQty * colPrice"); then just use the formula =frmExt

    Names with Workbook scope

    o Constants start with con (conBig "= 9E+307 + 8.97693134862315E+307 + 6E+292", conZzz "=rept(z, 255)")
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Registered User
    Join Date
    11-29-2008
    Location
    Houston
    MS-Off Ver
    Office 2003, and 2007
    Posts
    77

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    Shg,
    Ok...I have to ask, being an engineer and always interested in uniformity:
    Q: How does one go about getting one's hands on the "Bullen's PED" document? Is this the book http://www.amazon.com/Professional-E.../dp/0321262506 ?
    Q: Not sure I understand "...Fixed single cells..." If Cell N6 is "NetWeight" and is an integer, would it be "ptrNetWeight"?, What's the significance of "ptr..."?
    Q: In the case of my example above "TotNtWt = NetWeight x CylQty", would it be "frmTotNtWt=ptrNetWeight * ptrCylQty"
    Q: Do you provide any documentation at the beginning of your code with these details?, maybe in a separate code module?

    Also...I changed this thread to "Un-Solved" as it would appear that there's more valuable contributions coming.
    Last edited by DM2; 03-05-2015 at 08:29 PM.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,531

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    Q: In the case of my example above "TotNtWt = NetWeight x CylQty", would it be "frmTotNtWt=ptrNetWeight * ptrCylQty"
    These are variables defined and used in the VBA. So, no.

    shg is, I think, referring to naming conventions for named ranges, dynamic named ranges and named formulae.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using Names for Cells rather than "A1" and benifits for math calculations in a macro

    Quote Originally Posted by DM2 View Post
    Is this the book ...
    Yup.

    Q: Not sure I understand "...Fixed single cells..." If Cell N6 is "NetWeight" and is an integer, would it be "ptrNetWeight"?, What's the significance of "ptr..."?
    Q: In the case of my example above "TotNtWt = NetWeight x CylQty", would it be "frmTotNtWt=ptrNetWeight * ptrCylQty"
    Q: Do you provide any documentation at the beginning of your code with these details?, maybe in a separate code module?
    ptr is just the prefix for named single-cell ranges. As TMS says, this is my naming convention for ranges, constants, and formulas (what you see in Name Manager), not VBA variables. I also have a naming convention for those.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. replace all defined range names in the sheet that start with "Street" to "Road"
    By matrex in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 11-04-2016, 08:53 AM
  2. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  3. [SOLVED] Reverse personal names from "First Last" to "Last, First" and accommodate middle names
    By MrGadget6977 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 08-17-2013, 07:10 PM
  4. Excel Function that differentiates "male" from "female" names with 900+ names
    By doylehargrove in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-17-2012, 06:00 PM
  5. Replies: 6
    Last Post: 06-08-2012, 04:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1