Loop Through columns and make Named Ranges

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi there,
I'm trying to adapt something that Tonyyy and This is my Answer helped me with earlier.

I have a worksheet that has columns of data.
I would like to loop through the columns starting at the second one (B) and create a named range for the last 10 cells in each column.
So column 2 might get "_Range2" as a name for example.

Data looks like this
Book1
ABCDEFGHIJKLMNOPQRS
1DatePrice 1Price 2
212/26/20211627
312/27/20211227
412/28/20211724
512/29/20211526
612/30/20212625
712/31/20211724
81/1/20222125
91/2/20221326
101/3/20222627
111/4/20222526
121/5/20221527
131/6/20222225
141/7/20221827
151/8/20221726
161/9/20222625
171/10/20222525
181/11/20222225
191/12/20222227
201/13/20221324
21
22
23
24
25
Sheet1
Cell Formulas
RangeFormula
A2A2=TODAY()-10
A3:A20A3=A2+1


Ignore the formulas as I was just putting some random data in there.

The code I've tinkered with is here, but I can't quite get it to work.
I always have the same number of columns and would like to have named ranges for even blank column data.

VBA Code:
Sub RangeNamer()

Dim rng As Range
Dim y As Long
Dim z As Long
Dim RangeRef As Range
Dim rCount As Long

Sheets("Sheet1").Activate

y = 2    'price data starting in column 2
z = 1    'would like to name first column of price data as _Tick1 hence the 1

rCount = Cells(Rows.Count, 1).End(xlUp)

For Each rng In Range("B1:AY1").Columns

    Set RangeRef = Cells(rCount, y).End(xlUp).Offset(-10).Resize(11, 1)

    ThisWorkbook.Names.Add Name:="_Tick" & z, RefersTo:=RangeRef
   
z = z + 1
y = y + 1
Next rng
 
End Sub

Thanks!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
VBA Code:
Sub RangeNamer()

     Dim rng   As Range
     Dim z     As Long
     Dim RangeRef As Range
     Dim rCount As Long

     With Sheets("Sheet1")
          z = 1                                                 'would like to name first column of price data as _Tick1 hence the 1
          rCount = Cells(Rows.Count, 1).End(xlUp).Row           'last used row in column A

          For Each rng In .Range("B1:AY1").Cells                'loop through all this cells
               With .Cells(rCount, rng.Column).End(xlUp)        'starting from row rCount CTRL+up
                    Set RangeRef = .Offset(Application.Max(1 - .Row, -10)).Resize(11, 1)     'make sure to start at least in row 1 !!!!
                    'MsgBox RangeRef.Address
                    RangeRef.Name = "_Tick" & z
               End With
               z = z + 1
          Next rng
     End With
End Sub
 
Upvote 0
sorry, mistaken ! 2nd version
VBA Code:
Sub RangeNamer()
     Dim RangeRef As Range, c As Range

     With Sheets("Sheet1")
          Set c = .Cells(Rows.Count, 1).End(xlUp)
          Set RangeRef = c.Offset(Application.Max(2 - c.Row, -10)).Resize(Application.Min(11, Application.Max(1, c.Row - 1)))     'caution if number of rows <10
          MsgBox RangeRef.Address
          For Each rng In .Range("B1:AY1").Cells                'loop through all this cells
               RangeRef.Offset(, rng.Column - 1).Name = "_Tick" & rng.Column - 1
          Next rng
     End With
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub RangeNamer()
  Dim rng As Range, RangeRef As Range
  Dim y As Long, z As Long, rCount As Long
  
  Sheets("Sheet1").Activate
  z = 1    'would like to name first column of price data as _Tick1 hence the 1
  rCount = Range("A" & Rows.Count).End(xlUp).Row
  If rCount > 9 Then
    For y = 2 To Columns("AY").Column
      Set RangeRef = Cells(rCount, y).Offset(-9).Resize(10, 1)
      ThisWorkbook.Names.Add Name:="_Tick" & z, RefersTo:=RangeRef
      z = z + 1
    Next
  End If
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub RangeNamer()
  Dim rng As Range, RangeRef As Range
  Dim y As Long, z As Long, rCount As Long
 
  Sheets("Sheet1").Activate
  z = 1    'would like to name first column of price data as _Tick1 hence the 1
  rCount = Range("A" & Rows.Count).End(xlUp).Row
  If rCount > 9 Then
    For y = 2 To Columns("AY").Column
      Set RangeRef = Cells(rCount, y).Offset(-9).Resize(10, 1)
      ThisWorkbook.Names.Add Name:="_Tick" & z, RefersTo:=RangeRef
      z = z + 1
    Next
  End If
End Sub
Both BSALV and Dante’s methods work so I’m just selecting one for the solution. I’m learning quite a bit just from the replies and it’s interesting so see how people approach things differently sometimes.

Tonyyy pointed out that my original post of code works if I removed the .End(xlUp) from the RangeRef Set line (already done pre loop) and added .Rows to the end.

Thanks guys!
 
Upvote 0
Glad we could help & thanks for the feedback

It could be simplified like this:
VBA Code:
Sub rngname()
  Dim c As Range
  For Each c In Range("B1:AY1")
    ThisWorkbook.Names.Add "_Tick" & c.Column - 1, c.Cells(Range("A" & Rows.Count).End(3).Row).Offset(-9).Resize(10, 1)
  Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top