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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,235
Members
449,092
Latest member
SCleaveland

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