Question : Creating a dynamic range name using VBA

I am new to VBA programming and am having an issue attempting to dynamically name a range.  A simplified version of the problem code is illustrated below:

Sub NameaRange()

Dim ulr As Integer '(upper left row of the range)
Dim ulc As Integer '(upper left column of the range)
Dim lrr As Integer '(lower right row of range)
Dim lrc As Integer'Lower right column of range)

ulr = 5
ulc = 2
lrr = 10
lrc = 7

Names.Add Name:="ReportCopy1", RefersTo:="=Sheet1!cells($ulr,$ulc):cells($lrr,$lrc)"

End Sub

'Note that when I run this I get a Run time error" 1004 The formula you typed contains an error"


End Sub

Answer : Creating a dynamic range name using VBA

you can't use $ characters in strings to parse variables, you will need to do it this way
Names.Add Name:="ReportCopy1", RefersTo:="=Sheet1!cells(" & ulr & "," & ulc & "):cells(" & lrr & "," & lrc & ")"


but easier to just do this
Range(Cells(ulr, ulc), Cells(lrr, lrc)).Name = "ReportCopy1"
Random Solutions  
 
programming4us programming4us