For Looping look at this:
Code:
Sub GetRangeNames(pWkshtPathName As String)
On Error Resume Next
' Dim dbs As DAO.Database
' Dim rst As DAO.Recordset
Dim xlx As Object, xlw As Object
Dim nm
Dim NR_array() As String
Dim i As Integer 'counter for array
Dim blnEXCEL As Boolean 'Quit Excel at the end?
blnEXCEL = False
i = 0
' Establish an EXCEL application object
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
' Change True to False if you do not want the workbook to be
' visible when the code is running
' xlx.Visible = True
xlx.Visible = False
' the actual path and filename of the EXCEL file
Set xlw = xlx.Workbooks.Open(pWkshtPathName)
' loop thorugh range names, placing names into an array
For Each nm In xlw.Names
i = i + 1
ReDim Preserve NR_array(i)
NR_array(i) = nm.Name
' Debug.Print nm.Name
Next nm
' array now holds the range names
'
' other code to do something with the range names goes here
'
' Close the EXCEL file and clean up the EXCEL objects
xlw.Close False ' close the EXCEL file and don't save
DoEvents 'make time for close to complete
Set xlw = Nothing
If blnEXCEL = True Then
xlx.Quit
End If
Set xlx = Nothing
End Sub
Usage would be something like: (I used a button click)
Code:
Private Sub cmdRangeNames_Click()
Call GetRangeNames("F:\Forum\Sample Class Schedule.xlsx")
End Sub