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
Bookmarks