VBA: Create Multiple Named Ranges

mrMozambique

Board Regular
Joined
Mar 9, 2005
Messages
97
Hi all. I'm having trouble with some code to create a series of named ranges (one in each column for an indeterminate number of columns). It seems to be adding an unnecessary set of quotation marks (e.g. ="'tables'!$BP$4:$BP$101" instead of ='tables'!$BP$4:$BP$101). How can I remove the quotation marks? Thanks in advance for any help you can provide.

Code:
Sub MakeNamedRanges()

  Dim C As Long
  Dim Header As String
  Dim LastCol As Long
  Dim RefStr As String
  Dim Rng As Range
  Dim RngEnd As Range
  Dim Wks As Worksheet
  
    Set Wks = ActiveSheet
    
      LastCol = Wks.Cells(1, Columns.Count).End(xlToLeft).Column
        For C = 3 To LastCol
          Set Rng = Cells(4, C)
          Set RngEnd = Wks.Cells(Rows.Count, C).End(xlUp)
          Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
            RefStr = "'" & Wks.Name & "'!" & Rng.Address
            Header = Wks.Cells(3, C)
            On Error Resume Next
              Wks.Parent.Names(Header).Delete
              Err.Clear
            On Error GoTo 0
            Wks.Parent.Names.Add Header, RefStr
        Next C
        
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try this...

Code:
Wks.Parent.Names.Add Header, [COLOR="Red"]"=" & [/COLOR]RefStr



Also, you don't need this.
Code:
           On Error Resume Next
              Wks.Parent.Names(Header).Delete
              Err.Clear
            On Error GoTo 0
The .Add method in this case will overwrite the Name if it already exists.
 
Last edited:
Upvote 0
Or
Code:
For C = 3 To LastCol
    Set Rng = Cells(4, C)
    Set RngEnd = Wks.Cells(Rows.Count, C).End(xlUp)
    Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
    Header = CStr(Wks.Cells(3, C))
    Rng.Name = Header
Next C
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,943
Latest member
Newbie4296

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