VBA Code to Loop through and NameRange Cells

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Hello and thanks in advance and I will provide feedback to any assistance on solutions.

I'm trying to name range a large amount of cells and would like to use a VBA loop to do it. I am naming a column of cells from (20, 3) to (20, 69). That's 50 cells in column C starting on the 20th row and ending on the 69th row. Please note, these range names are only specific to "Sheet2" (a single worksheet) and not the workbook.

I'm getting errors such as "Error" "Object Required" on the line "If i < 10 Then Set RangeName = "T.0" & i" and "&" gets highlighted/

Here is my code thus far:

Code:
Sub NameRange1()


    Worksheets("Sheet2").Activate
        
        Dim i As Integer
        
        Dim LastRow As Long
            
    
        For i = 1 To 2
    
            Range(19 + i, 3).Select


            If i < 10 Then Set RangeName = "T.0" & i
            Else: Set RangeName = "T." & i
    
    
        Next i
    
    


End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Is this what you want?
Code:
Sub OilEconomist()
   Dim i As Integer
    
   With Worksheets("Sheet2")
      For i = 1 To 20
          .Cells(19 + i, 3).Name = Format(i, "T_00")
      Next i
   End With
End Sub
 
Upvote 0
Thank you for your response.

Although I haven’t tried it yet. It seems the code you’ve given is formatting the cells not “Naming” them with Range Name. If I was to manually do I select “Alt A” “M” “M” (Formulas and NameRange a Cell”

Any other solutions?
 
Upvote 0
Might be better to test the code posted before asking for other solutions ;)
 
Upvote 0
Thanks Mark858. Apologies and will do!

Fluff thanks so much!

That's close, but what I'm trying to do is name the cells T.01, T.02 ..... T.10, T.11..... T.20

With what you've given it's naming them T_01, T_02, etc. So I changed the "_" to a period and it's giving me T1.00, T2.00, .....T20.00

Also, it's making it refer to the entire workbook. How do you make it just for that specific sheet?
 
Upvote 0
How about
Code:
Sub OilEconomist()
   Dim i As Integer
    
   With Worksheets("Sheet2")
      For i = 1 To 20
          .Names.Add "T." & Format(i, "00"), "=Sheet2!R" & i & "C3"
      Next i
   End With
End Sub
 
Upvote 0
Thanks Fluff! That worked.

The only thing I did differently was to add 19 as such since I was starting on Row 20

Code:
Sub RangeName()
 
    Worksheets("Sheet2").Activate
       
        Dim i As Integer
        Dim ii
       
        With Worksheets("Sheet2")
       
        For i = 1 To 50
            
            
            .Names.Add "T." & Format(i, "00"), "=Sheet2!R" & (i + 19) & "C3"
        
            .Names.Add "F." & Format(i, "00"), "=Sheet2!R" & (i + 19) & "C4"
       
        Next i
  
    End With
   
 
End Sub
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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