+ Reply to Thread
Results 1 to 11 of 11

Loop Defining Named Ranges

  1. #1
    Registered User
    Join Date
    11-23-2009
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    32

    Loop Defining Named Ranges

    Hi,
    I'm trying to define some ranges with a loop but being a newbie at VBA I'm finding it difficult to solve the error in the code.
    First, as the data area is uncertain (variation in the number of rows and columns) i look to the end of the entire data area. Then i want to define intervals of 250 rows with a 30 row difference between each of them. Here is my code:

    Please Login or Register  to view this content.
    The error that I'm getting is that the name of the range is incorrect. Does anyone have a suggestion?
    Thanks in advance.

  2. #2
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208

    Re: Loop Defining Named Ranges

    Just to be clear, you want to define several different named ranges, each having 250 rows, and the new named range starting 30 rows after the previous? If that is the case, try:

    Please Login or Register  to view this content.
    HTH,
    Jason

  3. #3
    Registered User
    Join Date
    11-23-2009
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Loop Defining Named Ranges

    Jason,
    Thanks for the suggestion, i have learned somethings with the code you posted and cleaned up some other code.

    In regard to my problem that's exactly what i want to do: "different named ranges, each having 250 rows, and the new named range starting 30 rows after the previous".

    Unfortunately, although the error has disappeared, i only get two named ranges: TP and TPy. I believe that the
    Please Login or Register  to view this content.
    part of the code isn't working as i want. I wanted to have named ranges as TP1, TP2, TP3 and so on.
    Do you have any idea why? Thank for the help.

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208

    Re: Loop Defining Named Ranges

    Not sure, it worked for me. What are the values of LastRowSh1 and LastColSh1? Can you post up a copy of your workbook (assuming it doesn't have any sensitive data in it)? Or can you post the full code that you are currently using?

  5. #5
    Registered User
    Join Date
    11-23-2009
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Loop Defining Named Ranges

    jasoncw,
    Thanks again.
    I have outputted to a cell the values of LastRowSh1 and LastColSh1 and they are the expected ones.

    Here is the file, it's just stock prices ordered by date. http://www.sendspace.com/file/4x1i8m

    With the code like this
    Please Login or Register  to view this content.
    i get an error (Translated i think it's "Number of Arguments is Incorrect") so i tried like this
    Please Login or Register  to view this content.
    but just get two ranges: TP and TPy.

    Thanks for any comment.

  6. #6
    Registered User
    Join Date
    11-23-2009
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Loop Defining Named Ranges

    I have also notice something, i don't know if the fact that it jumps 30 rows every time it defines a range it would never be the exact same number of LastRowSh1 in
    Please Login or Register  to view this content.
    .

  7. #7
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208

    Re: Loop Defining Named Ranges

    It appears as though Excel 2007 does not like the named ranges "TPx", which I think is the stem of your problems (see attached screen print below when I went to save the file after running the procedure). It worked for me, as I am running Excel 2003.

    Try this for your procedure. Hopefully it resolves the issue:

    Please Login or Register  to view this content.
    Attached Images Attached Images
    Last edited by jasoncw; 11-23-2009 at 09:06 PM.

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Loop Defining Named Ranges

    TP1 ,TP2 etc are cell addresses (in Excel 2007) and illegal names.
    Try "TP" & y & "a"
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  9. #9
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208

    Re: Loop Defining Named Ranges

    Well, that makes sense now. Thanks for clearing it up.

    So you can use my method above (using an underscore before or after "TP"), or the one suggested above, entering an "a" after the reference.

  10. #10
    Registered User
    Join Date
    11-23-2009
    Location
    Porto, Portugal
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Loop Defining Named Ranges

    Thanks a lot to both of you. It now running ok and defining all the ranges.

  11. #11
    Registered User
    Join Date
    04-10-2017
    Location
    Texas
    MS-Off Ver
    Office for Mac
    Posts
    1

    Re: Loop Defining Named Ranges

    Sorry to bump an old thread but THANK YOU SO MUCH. I spent an hour googling this same issue to no avail and this thread finally solved my issue. To think all I needed to do was add an underscore as to not confuse Excel....

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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