+ Reply to Thread
Results 1 to 4 of 4

Range Reference formula

  1. #1
    Registered User
    Join Date
    12-29-2011
    Location
    San Diego, Ca.
    MS-Off Ver
    Excel 2007
    Posts
    60

    Range Reference formula

    Range references are strings such as A5 in Range ("A5") as I understand it. Suppose I create a range reference including simple math. For example, (and ignore the select because I generally avoid this but it isn't relevant to the question).

    Please Login or Register  to view this content.
    Question: the reference "D" is a string but x is an integer which means mixing a string with an integer. This seems to work but I'm not sure why. It would seem that the (x+3) result needs to be converted to a string first and then concantenated with the literal "D." Is this really necessary and if not, why not?

    Thanks in advance.
    Last edited by Leith Ross; 01-23-2020 at 11:55 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Range Reference formula

    Hello gototcm,

    VBA treats columns as either a number or a letter. If it is a letter then VBA internally converts this to a number when compiling your code.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    10-05-2019
    Location
    Work: East Coast; Home: Texas, USA
    MS-Off Ver
    Mostly 2010 ( Windows 10 & 365 Insider Super Fast )
    Posts
    32

    Re: Range Reference formula

    Sometimes parts of VBA have been written to take arguments / parameters as strings, that is to say within a string.
    Such as in this case, the only thing that VBA is really interested in is the actual
    A8
    That is the information that it needs.
    If the syntax had been written to accept the information in this form Range(A8) , then you would be limited to having to hard code, that is to say, put in the exact values, A8
    Because the syntax has been written to take the information within a string, then you are more flexible and can make the coding dynamic, as you did, since in your example , x could be filled by some other coding previously.
    AFAIK, there is no special reason why the syntax needed to be written such as to take this form, Range("A8") , rather than this form Range(A8), but it gives you that extra flexibility.
    Often you may be able to use an alternative syntax. For example, in coding you might sometimes see
    [A8]
    Which is looked at by VBA as
    [=A8]
    and can often be used as an alternative to
    Range("A8").Value

    Molly

    Edit: Note that usually, but not always , Range("A8") defaults to Range("A8").Value

    ( Another good tip when developing coding of this form is to use a string variable for the argument which needs to be a string, then take a look at it in the immediate window ( https://www.excelcampus.com/vba/vba-immediate-window-excel/ )
    The form that is printed out in the immediate window, is “as VBA “Sees” it”. So that can be a good indication as to whether you have got it right

    Dim strArg as String, x as long
    X=8
    strArg ="A" & x & ""
    debug.Print strarg

    In the immediate window you would see
    A8
    without any quotes. You see what VBA sees.

    In general , or often, a pair of enclosing " "s tells VBA to take what is within the quotes literally. So in the above example vba takes OR "sees"
    A
    When it comes to x , it sees no enclosing " "s so it takes the value in the variable x , which is
    8

    Then the & is the Vba syntax to "stick things together"
    So the result is
    A8


    ( Edit later : just for others info .. I think this thread is related to this 'un https://www.excelforum.com/excel-programming-vba-macros/1303728-contiguous-range-reference.html
    )
    )
    Last edited by MollyBrennholz; 01-24-2020 at 09:45 AM.

  4. #4
    Registered User
    Join Date
    12-29-2011
    Location
    San Diego, Ca.
    MS-Off Ver
    Excel 2007
    Posts
    60

    Re: Range Reference formula

    Thanks. That is now clear.

+ 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. [SOLVED] Range reference in r1c1 formula
    By redhill in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-27-2019, 08:19 AM
  2. [SOLVED] COUNTIF formula where part of the range reference needs to use the result of a formula
    By zookeepertx in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 11-09-2018, 05:39 PM
  3. [SOLVED] Formula Range to reference cell value
    By Simon.xlsx in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-18-2016, 11:37 AM
  4. Trying to put cell range reference in formula
    By corwinjk in forum Excel General
    Replies: 2
    Last Post: 05-15-2012, 06:02 PM
  5. Replies: 8
    Last Post: 04-01-2012, 02:45 AM
  6. Increment range reference in formula
    By XL_Newbie in forum Excel General
    Replies: 4
    Last Post: 12-08-2009, 01:46 AM
  7. Formula to look up a range reference?
    By andrewc in forum Excel General
    Replies: 2
    Last Post: 03-04-2008, 11:44 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