Excel VBA - setting scope of Named Range to Worksheet problem

This works, but the Named Range has a 'Workbook' scope, even though the range is qualified to ActiveSheet. I'm stuck because the macro recordser version uses RefersTo. This is simper.
How can I make it 'Worksheet' scope (using this syntax version, if possible) please?

Private Sub btnOk_Click()

    Dim strSourceRange As String
    Dim intOffsetColumns As Integer
    Dim wksActiveWorksheet As Worksheet
    Dim rngSourceRange As Range
    strSourceRange = Me.refSourceRange.Value
    Set wksActiveWorksheet = ActiveSheet
    Set rngSourceRange = wksActiveWorksheet.Range(strSourceRange)
    intOffsetColumns = Me.txtOffsetColumns
    iCol = 1
    For iRow = 1 To rngSourceRange.Rows.Count - 1

        strName = "TRAC_" + rngSourceRange.Cells(iRow, 1).Value
        rngSourceRange.Cells(iRow, 1).Offset(0, intOffsetColumns).Name = strName

    Next iRow

End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertFabrice LambertCommented:

Not sure to understand where you named range is referenced.

Other things I noticed:
Do you really need the activesheet ?
what if another sheet is selected ?

If yes, you'd better set your wksActiveWorksheet object to thisworkbook.worksheets(thisworkbook.activesheet.name), at least you'll obtain a stable and reliable object.
hindersalivaAuthor Commented:
Fabrice, yes I was initially trying that syntax. See here

The solution I got from there was the other syntax and it worked. But not the 'worksheet' scope I wanted.

The cope will be in an AddIn and will run on the ActiveWorkbook.ActiveSheet.
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may try something like this then...
strName = "TRAC_" & rngSourceRange.Cells(iRow, 1).Value
wksActiveWorksheet.Names.Add Name:=strName, RefersToR1C1:="=" & rngSourceRange.Cells(iRow, 1).Offset(0, intOffsetColumns).Address(ReferenceStyle:=xlR1C1)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fabrice LambertFabrice LambertCommented:
Subodh's comment might work.

I can only tell you to be extremly carefull with the ActiveWorksheet object, as it is a very evil beast.
hindersalivaAuthor Commented:
Genius! Thanks Neeraj.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.