+ Reply to Thread
Results 1 to 8 of 8

Ranges with same address behaving differently with offset

  1. #1
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Ranges with same address behaving differently with offset

    I can probably work around this, but I don't understand why it is happening.
    The attached spreadsheet has everything set up. Here is the code:
    Please Login or Register  to view this content.
    Here is the output:
    badr address is: $D$1:$F$7
    goodr address is: $D$1:$F$7
    badr(2) address is: $E$1:$E$7
    goodr(2) address is: $E$1

    I don't understand how these two ranges can have the same address, yet behave differently when using the (2) offset. I thought it always went by cell (and that is what my other code was expecting).

    Thanks
    Attached Files Attached Files
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Ranges with same address behaving differently with offset

    Possibly in this line of code
    Please Login or Register  to view this content.
    goodr is a Variant. Try changing it to a range.

  3. #3
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Ranges with same address behaving differently with offset

    Well, the solution is to use
    badr.cells(2).address
    But, I'm still curious what is going on internally to yield the different results (and if there is a way to test for it).

    Thanks.

  4. #4
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Ranges with same address behaving differently with offset

    @dangelor
    Interesting. I thought using comma delimiters allowed me to assign many variables on one line.
    From: https://docs.microsoft.com/en-us/dot.../dim-statement
    Declaring Multiple Variables
    You can declare several variables in one declaration statement, specifying the variable name for each one, and following each array name with parentheses. Multiple variables are separated by commas.

    Please Login or Register  to view this content.


    However, putting them as separate Dim xxxx as Range lines of code made them act similarly (goodr now matches badr), so at least now there is some consistency.
    Last edited by Pauleyb; 02-08-2018 at 11:44 AM.

  5. #5
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Ranges with same address behaving differently with offset

    I agree with @dangelor.

    goodr is a variant type. While a range can be treated as an array like you are doing (never seen anyone do this, I always use range.offset) the variant likely cannot. So the range type "offsets" the whole range were as the variant may only offset the first cell of the range.

    Please Login or Register  to view this content.
    Try the above and see if it works as you had posted it. Other wise use the methods of the range object to accomplish this (.offset, .resize, etc)
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  6. #6
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Ranges with same address behaving differently with offset

    You can declare multiple variables on a single line, but just like declaring them 1 per line, if you dont give them a type they default to variant.

    Also, the link you gave is for Visual Basic, not VBA...they are very different.

  7. #7
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Ranges with same address behaving differently with offset

    Ok - thanks. Mystery solved. VB vs VBA and variant instead of Range. Bravo

    Yep - here it is for VBA:
    https://msdn.microsoft.com/en-us/vba...ring-variables

    Thanks.
    Pauley

  8. #8
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Ranges with same address behaving differently with offset

    Pulled from the article:

    You can declare several variables in one statement. To specify a data type, you must include the data type for each variable. In the following statement, the variables intX, intY, and intZ are declared as type Integer.

    Please Login or Register  to view this content.
    In the following statement, intX and intY are declared as type Variant, and only intZ is declared as type Integer.

    Please Login or Register  to view this content.
    No worries we have all done it at one point or another.

+ 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] Macro behaving differently depending on how it is called
    By louiserace in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-13-2016, 09:53 AM
  2. [SOLVED] VBA code behaving differently when stepping
    By Durarara in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-18-2016, 05:20 PM
  3. [SOLVED] 2 copies of Excel 2010 behaving differently.
    By JennyW1983 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-09-2015, 04:36 AM
  4. Replies: 9
    Last Post: 07-17-2015, 04:08 PM
  5. [SOLVED] Enable / Disable Macros behaving differently in Excel 2003 from Windows XP to Windows 7
    By TC1980 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-21-2013, 09:09 AM
  6. OFFSET behaving oddly
    By Ryan Poth in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-30-2006, 02:30 AM
  7. need help using the offset function differently
    By Marshin in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-16-2005, 12:06 PM

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