Excel VBA Add Named ranges with passed through sheet names.

Rowland Hamilton

Active Member
Joined
Nov 13, 2009
Messages
250
How can I replace the sheet name with a passed through named variable?

Code:
MasterWB.Names("HC_Results").RefersToR1C1 = _
        "=OFFSET(Results!R1C1,0,0,COUNTA(Results!C1),COUNTA(Results:Results!R1))"

Does't work when I just do this:
Code:
MasterWB.Names("HC_" & wsR & "").RefersToR1C1 = _
        "=OFFSET(" & wsR & "!R1C1,0,0,COUNTA(" & wsR & "!C1),COUNTA(" & wsR & ":" & wsR & "!R1))"

I have the sheet defined with

Code:
Sub Update_NamedRange(wsR As Worksheet, MasterWB As Workbook)

And the code passes from:
Code:
Sub START_IT_YALL()
Dim wsR As Worksheet 'Results worksheet
Dim MasterWB As Workbook
    Set MasterWB = ThisWorkbook
    Set wsR = Worksheets("Results")
    Call Update_NamedRange(wsR, MasterWB)
End Sub

Here is full code:
Code:
Option Explicit
Option Compare Text

Function NamedRangeExists(strName As String, _
    Optional wbName As String) As Boolean
     'Declare variables
    Dim rngTest As Range, i As Long
     
     'Set workbook name if not set in function, as default/activebook
    If wbName = vbNullString Then wbName = ActiveWorkbook.Name
     
    With Workbooks(wbName)
        On Error Resume Next
         
         'Loop through all sheets in workbook.  In VBA, you MUST specify
         ' the worksheet name which the named range is found on.  Using
         ' Named Ranges in worksheet functions DO work across sheets
         ' without explicit reference.
        For i = 1 To .Sheets.Count Step 1
             
             'Try to set our variable as the named range.
            Set rngTest = .Sheets(i).Range(strName)
             
             'If there is no error then the name exists.
            If Err = 0 Then
                 
                 'Set the function to TRUE & exit
                NamedRangeExists = True
                Exit Function
            Else  'Clear the error
                Err.Clear
            End If
        Next i
    End With
End Function
 
Sub NREtest1()
    MsgBox NamedRangeExists("test")
End Sub
 
Sub NREtest2()
    MsgBox NamedRangeExists("testing")
End Sub
Sub Update_NamedRange(wsR As Worksheet, MasterWB As Workbook)
     'I want to create a Named Range if it doesn't exist already ..
    If NamedRangeExists("HC_Results") Then
        MasterWB.Names("HC_Results").RefersToR1C1 = _
        "=OFFSET(Results!R1C1,0,0,COUNTA(Results!C1),COUNTA(Results:Results!R1))"
        
        '  ActiveWorkbook.Names("HC_Results").RefersToR1C1 = _
        "=OFFSET(Results!R1C1,0,0,COUNTA(Results!C1),COUNTA(Results!R1))"
    Else
        MasterWB.Names.Add Name:="HC_Results", RefersToR1C1:= _
        "=OFFSET(Results!R1C1,0,0,COUNTA(Results!C1),COUNTA(Results!R1))"
    End If
End Sub
Sub START_IT_YALL()
Dim wsR As Worksheet 'Results worksheet
Dim MasterWB As Workbook
    Set MasterWB = ThisWorkbook
    Set wsR = Worksheets("Results")
    Call Update_NamedRange(wsR, MasterWB)
End Sub
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
When I separate with ampersands, and type in the sheet name it works but when I use the variable it does not.

This works:
Code:
MasterWB.Names("HC_Results").RefersToR1C1 = _
        "=OFFSET(" & "Results" & "!R1C1,0,0,COUNTA(" & "Results" & "!C1),COUNTA(" & "Results" & "!R1))"

But I want to use the pass through (doesn't work):
Code:
MasterWB.Names("HC_Results").RefersToR1C1 = _
        "=OFFSET(" & """ & wsR & """ & "!R1C1,0,0,COUNTA(" & "" & wsR & """ & "!C1),COUNTA(" & """ & wsR & """ & "!R1))"
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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