+ Reply to Thread
Results 1 to 21 of 21

Loop through columns and named ranges

Hybrid View

  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).

    Function findReportNamedRange(reportColCount As Integer) As Integer                                    '6
        
        Dim reportSheet As Worksheet
        Dim reportCol As Integer
        Dim namedRange As Name
    
        Set reportSheet = Sheets("Report")
        
            For reportCol = reportColCount - 1 To 1 Step -1
                Debug.Print reportCol                                                                                                        '5
                For Each namedRange In ThisWorkbook.Names
                    Debug.Print namedRange                                                                                               'prints _xlfn.IFERROR
                    If Not Intersect(reportSheet.Cells(3, reportCol), namedRange.RefersToRange) Is Nothing Then
                        findReportNamedRange = reportCol
                        Debug.Print findReportNamedRange                                                                              'should be 1 but doesn't print
                        Exit For
                        Exit For
                    End If
                Next
            Next reportCol
    
    End Function
    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,701

    Re: Loop through columns and named ranges

    Maybe:

    ?Range("nrTest").Address
    $A$1:$B$6
    ?Range("nrTest").Cells(1,1).Address
    $A$1
    ?Range("nrTest").Cells(1,1).EntireColumn.Address
    $A:$A

    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,701

    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

    ?Range("nrTest").Address
    $A$1:$B$6
    ?Range("nrTest").Cells(1,1).Address
    $A$1
    ?Range("nrTest").Cells(1,1).EntireColumn.Address
    $A:$A
    Range("A2:F2").Select
    ?Intersect(Selection,Range("nrTest")).Address
    $A$2:$B$2
    ?Range("nrTest").Cells(1,1).Column
     1 
    

    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,701

    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
        
    For Each namedRange In ThisWorkbook.Names
            namedRange.Delete
    Next
    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,701

    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:

    _xlfn.IFERROR =#NAME? =#NAME? True
     1004         
                  The name that you entered is not valid.
    
    Reasons for this can include:
        -The name does not begin with a letter or an underscore
        -The name contains a space or other invalid characters
        -The name conflicts with an Excel built-in name or the name of another object in the workbook

  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,701

    Re: Loop through columns and named ranges

    Another little example:

    Sub sListNames_2()
    
    Dim NR As Name
    On Error Resume Next
    
    Range("C4").Name = "nrVisible"
    Range("C6").Name = "nrHidden"
    For Each NR In ThisWorkbook.Names
        Debug.Print "(1)", NR.Name, NR.Visible
    Next
    
    For Each NR In ThisWorkbook.Names
        If NR.Name = "nrHidden" Then
            NR.Visible = False
        End If
    Next
    
    For Each NR In ThisWorkbook.Names
        Debug.Print "(2)", NR.Name, NR.Visible
    Next
    
    For Each NR In ThisWorkbook.Names
        NR.Delete
    Next
    
    For Each NR In ThisWorkbook.Names
        NR.Visible = True
    Next
    
    For Each NR In ThisWorkbook.Names
        Debug.Print "(3)", NR.Name, NR.Visible
    Next
    
    On Error Resume Next
    
    End Sub

    Regards, TMS

  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,701

    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
    =IFERROR(A2/B2,"if error used here")


    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.

    Sub sListNamedRanges()
    
    Dim NR As Name
    For Each NR In ThisWorkbook.Names
        Debug.Print NR.Name; " "; NR.RefersTo; " "; NR.RefersToLocal; " "; NR.Visible
        NR.Visible = True
        On Error Resume Next
        NR.Delete
        If Err.Number <> 0 Then
            Debug.Print Err.Number, vbCr, Err.Description
        End If
        On Error Resume Next
    Next NR
    
    End Sub

    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 :

    Function findReportNamedRange(reportColCount As Integer) As Integer
        
        Dim reportSheet As Worksheet
        Dim reportCol As Integer
        Dim namedRange As Name
    
        Set reportSheet = Sheets("Report")
        
            For reportCol = reportColCount - 1 To 0 Step -1 'in this case 5
                For Each namedRange In ThisWorkbook.Names
                    If namedRange.Name <> "_xlfn.IFERROR" Then
                        If Not Intersect(reportSheet.Cells(3, reportCol), namedRange.RefersToRange) Is Nothing Then
                            findReportNamedRange = reportCol
                            Debug.Print findReportNamedRange    'outputs 1 - indeed Range("A3:A33") is a named range
                            Exit For
                            Exit For
                        End If
                    End If
                Next
            Next reportCol
            
    End Function
    This is probably unrelated but when I call the function with this :

    Dim configCol as integer
    
    configCol = findReportNamedRange(reportColCount)
                    Debug.Print configCol 'outputs 0
    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,701

    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 :
    Debug.Print "Before calling function : " & reportColCount
    configCol = findReportNamedRange(reportColCount)
    Debug.Print "Number obtained by calling function : " & configCol
    Function:
    Function findReportNamedRange(reportColCount As Integer) As Integer
        
        Dim reportSheet As Worksheet
        Dim reportCol As Integer
        Dim namedRange As Name
    
        Set reportSheet = Sheets("Report")
        
            For reportCol = reportColCount - 1 To 0 Step -1
                Debug.Print "Looping through : " & reportCol
                For Each namedRange In ThisWorkbook.Names
                    If namedRange.Name <> "_xlfn.IFERROR" Then
                        If Not Intersect(reportSheet.Cells(3, reportCol), namedRange.RefersToRange) Is Nothing Then
                            findReportNamedRange = reportCol
                            Debug.Print "The value obtained: " & findReportNamedRange
                            Exit For
                            Exit For
                        End If
                    End If
                Next
            Next reportCol
            
    End Function
    Immediate window:
    Before calling function : 6
    Looping through : 5
    Looping through : 4
    Looping through : 3
    Looping through : 2
    Looping through : 1
    The value obtained: 1
    Looping through : 0
    Number obtained by calling function : 0

  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,701

    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. [SOLVED] 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