Prevost
Board Regular
- Joined
- Jan 23, 2014
- Messages
- 198
Hi There. I am trying to create named ranges. All the range names are in the first row and below them is their respective range entries (the cell immediately below the name cell is intentionally left blank). I check the first row cells until I get to the end (where an empty cell is) to get the number of ranges. Then I search down the column, starting at the first column, until I reach an empty cell to determine the range length. There is an error when I attempt to assign the range name to the Cell containing the text. The code won't work when
[Name:= Cells(1,y)].......but I don't get an error when the code is [Name:= "String Text"].......but obviously I don't want every range to be named the same.
Any help is greatly appreciated!
[Name:= Cells(1,y)].......but I don't get an error when the code is [Name:= "String Text"].......but obviously I don't want every range to be named the same.
Any help is greatly appreciated!
Code:
Sub CreateRanges()
Dim i As Integer, j As Integer, y As Integer, x As Integer
Dim NewRange As Range
i = 3: j = 1
Do While Cells(1, j) <> vbNullString
j = j + 1
Loop
For y = 1 To j
Do While Cells(i, y) <> vbNullString
i = i + 1
Loop
Set NewRange = ActiveSheet.Range(Cells(3, y), Cells(i - 1, y))
ActiveWorkbook.Names.Add Name:=Cells(1, y), RefersTo:=NewRange 'this line is highlighted when I click Debug
y = y + 1
Next
End Sub
Last edited: