Question : Set range name dynamically in VBA Excel

I have a procedure that loops through the workbook and lists all the account numbers from the worksheet tab.  These vary every month and I need to create a named range (which is used in an import into an ACCESS 2003 database) that dynamically is created based on the number of accounts in the column.  One month could be 35 the next, 125.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
Public Sub CreateWorksheetList()
'This is done when the data is exported just in case there is a change in the reports
'that are being created.  It is used to check against the database if there
'are any new accounts that have not been entered there.
Dim rngTOC As Range, rngWksName As Range
Dim rngAccount As Range

    Set wksTOC = Worksheets("Table_Of_Contents")
    wksTOC.Activate
    Set rngTOC = Range("Z2")
        For Each wks In Worksheets
            If wks.Name Like "Acct_*" Then
        Set rngWksName = rngTOC.Offset(1, 0)
            rngTOC.Value = CStr(Right(wks.Name, 6))
        Set rngTOC = rngWksName
            End If
        Next
        
       Set rngAccount = wksTOC.Range("Z2").End(xlDown).Offset(0, 0).Select
        
End Sub

Answer : Set range name dynamically in VBA Excel

this code will create a named range visible in the name manager, the name of the range is Target, it looks similar to what you said you tried but it works for me so give it a shot

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Sub Macro1()

Dim target As Range

Range(ActiveCell, ActiveCell.End(xlDown)).Select
Set target = Application.Selection
    
ActiveWorkbook.Names.Add Name:="Target", RefersTo:=target
ActiveWorkbook.Names("Target").Comment = ""

End Sub
Random Solutions  
 
programming4us programming4us