+ Reply to Thread
Results 1 to 21 of 21

Loop through columns and named ranges

  1. #1
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Loop through columns and named ranges

    Hi,

    I'm trying to find the column number of a named range (which has been defined previously in my code).

    Please Login or Register  to view this content.
    Am I missing something obvious here ?

    Thanks,
    amphi

  2. #2
    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
    44,464

    Re: Loop through columns and named ranges

    Maybe:

    Please Login or Register  to view this content.

    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


  3. #3
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Loop through columns and named ranges

    Thanks TMS. Not sure what your suggestion should do/check ?
    Depending with what I call the function, the column returned could be 1 (first time calling the function) then could be any number, 5,9,15 etc.

  4. #4
    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
    44,464

    Re: Loop through columns and named ranges

    I'm not really sure what you are trying to achieve.

    The code examples below show you how to get the address details of a Named Range

    Please Login or Register  to view this content.

    The question mark is short-hand, in the Immediate Window, for Debug.Print


    Regards, TMS

  5. #5
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Loop through columns and named ranges

    Thanks I'll try work through it using your tips !

  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
    44,464

    Re: Loop through columns and named ranges

    You're welcome. Thanks for the rep.

  7. #7
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Loop through columns and named ranges

    One of the reasons this isn't working on my workbook is that I have an issue with _xlfn.iferror = #NAME as a hidden name stored in my workbook. The reason is that I've imported a 2007 sheet and added an IFERROR formula. I've tried deleting the name
    Please Login or Register  to view this content.
    but, as it's a hidden name, the loop doesn't access it. However when I loop through the names and debug.print, it still appears.

    Source of the issue : http://www.pcreview.co.uk/threads/_x...nager.3434850/

  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
    44,464

    Re: Loop through columns and named ranges

    I'm intrigued. Can you post a sample workbook with the Named Range(s)?

    It looks as though you've consulted the best ... but I'd still like to have a go

    I put a formula into a cell to generate an IFERROR condition.
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I then used this code to list the Named Ranges. In this instance, there was only one ... the one generated by using IFERROR. Seems like you can't delete it but you only get the one, regardless of how many times you use IFERROR.

    Please Login or Register  to view this content.

    Regards, TMS

  9. #9
    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
    44,464

    Re: Loop through columns and named ranges

    And, I should have made the point ... it's not because it's a hidden name that you can't delete it, it's because it's system generated. Even if you remove the formula generating and trapping the error, it is still present.

    Debug Output:

    Please Login or Register  to view this content.

  10. #10
    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
    44,464

    Re: Loop through columns and named ranges

    Another little example:

    Please Login or Register  to view this content.

    Regards, TMS

  11. #11
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Loop through columns and named ranges

    Appreciate the thoroughness ! Yeah I quickly realized that I wouldn't be able to delete the name because as you rightly mentioned, it was system generated. Luckily enough, I simply trapped the name with an IF statement and added the name as string which seemed to do the trick :

    Please Login or Register  to view this content.
    This is probably unrelated but when I call the function with this :

    Please Login or Register  to view this content.
    it seems that the function looses it's value.

  12. #12
    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
    44,464

    Re: Loop through columns and named ranges

    You're welcome.

    With regard to the last question, I haven't got anything to test the function, or its call, with. Sorry, I'm not going to try to second guess what data is there in order to test it.

    Regards, TMS

  13. #13
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Loop through columns and named ranges

    Sure no worries. At least the _xlfn.IFERROR issue is solved

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop through columns and named ranges

    Is the function actually returning a value?
    If posting code please use code tags, see here.

  15. #15
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Loop through columns and named ranges

    This is what I'm getting :

    Call :
    Please Login or Register  to view this content.
    Function:
    Please Login or Register  to view this content.
    Immediate window:
    Please Login or Register  to view this content.

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop through columns and named ranges

    Can you upload a sample workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  17. #17
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Loop through columns and named ranges

    Here you go.
    Attached Files Attached Files

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop through columns and named ranges

    Replace the 2 Exit Fors with Exit Function, the second Exit For is never executed and only the inner loop is exited.

    By the way, why is reportCol looping down to 0? If 0 is ever reached you'll get an error with reportSheet.Cells(3, reportCol).

    PS Even if the 2nd Exit For was executed I don't think it would apply to the outer loop.
    Last edited by Norie; 06-02-2015 at 09:50 AM.

  19. #19
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Loop through columns and named ranges

    All those debug.prints did show me the loop wasn't exiting but i never knew about Exit Function..
    Yes I started getting an error in the sample work book I realized it was a mistake.
    Thanks Norie !

  20. #20
    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
    44,464

    Re: Loop through columns and named ranges

    I agree with Norie's solution.

    Can you clarify the purpose of this routine? Why do you pass it a counter? Are you just trying to limit it? If I add a second Named Range, in say, column 3, the routine will exit with a result of 3 with no mention or reference to the Named Range in column 1.

    Is it possible that you might have a Named Range outside the bounds you set?

    Regards, TMS

  21. #21
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Loop through columns and named ranges

    Yes the counter is to limit the routine and yes, I also set Named Ranges outside the function & the call. Bigger routine is this: I'm importing sheets, adding summaries from them on a "Report" sheet. On the "Report" sheet, Range("A3:A33") are labels. First sheet I import would occupy Range("B3:F33") then Range("G3:G33") would be a Named Range and would sum Range("B3:F33"). Range("H3:L33") would be occupied by the summary of the next sheet. Range("M3:M33") would be named and then sum Range("H3:L33").
    Last edited by amphinomos; 06-03-2015 at 03:32 AM.

+ 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. Loop Defining Named Ranges
    By lopiner in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-20-2017, 02:57 PM
  2. Replies: 2
    Last Post: 10-18-2013, 01:56 PM
  3. VBA to create specific named ranges and repeating process (Loop?) across columns
    By vinothj86 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-30-2012, 05:01 AM
  4. Multiple Named Ranges Pasting Loop issue
    By pacman_d in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-13-2010, 09:46 PM
  5. named ranges in a For Loop
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-22-2005, 11:10 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