Named Range using VBA

cfree36

Board Regular
Joined
Oct 5, 2005
Messages
175
I have the following code:

Code:
Sub NameRange()
Dim WS As Worksheet

counter = 1

If counter = 51 Then Exit Sub

For Each WS In ActiveWorkbook.Worksheets
    If WS.Visible = xlSheetVisible Then
    WS.Activate
    ActiveCell.Cells.Select
    ActiveWorkbook.Names.Add Name:="vendor" & counter, RefersToR1C1:="='" & counter & "'!R1:R65536"""
    counter = counter + 1
    End If
Next WS
End Sub

However I get an error on the RefersToR1C1 line.

The counter changed each Name to Vendor1, Vendor2, etc....
The sheets are named 1, 2, 3, etc...
I need the named range to refer to the correct sheet

Vendor1 should refer to Sheet Named '1' R1:R65536
Vendor2 should refer to Sheet Named '2' R1:R65536
etc.

Any help here would be great... Thanks in advance.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,351
Office Version
  1. 365
Platform
  1. Windows
Try this.
Code:
Sub NameRange()
Dim WS As Worksheet

    For Each WS In ActiveWorkbook.Worksheets
        If WS.Visible = xlSheetVisible Then
            counter = counter + 1
            WS.Range("A1:IV65536").Name = "vendor" & counter
        End If
    Next WS
    
End Sub
 

Forum statistics

Threads
1,171,969
Messages
5,878,542
Members
433,351
Latest member
Cindy Little

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
Top