Question : Macro that will create 52 otherwise identical named ranges, each with a different number in the middle.

I have a range named Tails1 with this definition:
Tails1='[ACA Week 24.xls]TailAvailability'!$C$6:$R$204

I need a macro that will create names for the remaining 51 weeks of the year, each with a different week number, for example:
Tails2='[ACA Week 2.xls]TailAvailability'!$C$6:$R$204
Tails17='[ACA Week 17.xls]TailAvailability'!$C$6:$R$204

If necessary I can rename the first 9 files with two digit numbers, but I'd rather keep them titled as is. (Although come to think of it, it would be nice to know it the two digit way as well, in case someone else decides they want the files all named with two digits.)

Thanks,
John


Answer : Macro that will create 52 otherwise identical named ranges, each with a different number in the middle.

OK, to set the named range to an external workbook, try this code:

Be sure to change the filepath to where your "ACA Week x" workbooks are located in this line:

filePath = "C:\"
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Sub CreateNamedRangesExtWbk()
'Create Named Range in External Workbook
    
    Dim wb As Workbook
    Dim filePath As String
    Dim currentWbk As String
    
    filePath = "C:\"
    
    Application.ScreenUpdating = False
    currentWbk = ActiveWorkbook.Name
    
    For i = 1 To 52
        Set wb = Workbooks.Open(filePath & "ACA Week " & i)
        Workbooks(currentWbk).Names.Add Name:="Tails" & i, RefersTo:=wb.Worksheets("TailAvailability").Range("$C$6:$R$204")
        wb.Close False
        Set wb = Nothing
    Next

    Application.ScreenUpdating = True
    
    MsgBox "Named Ranges have been set!"

End Sub
Random Solutions  
 
programming4us programming4us