Dynamic named range - problem when sheet's name renames!

jakuza

New Member
Joined
Jul 9, 2012
Messages
36
I create a dynamic named range for certain cells in one of my sheets. The problem is when user changes the name of that sheet. So, I decide to make VBA code which will run when sheet changes, but I can not make it done right.

My dynamic named range is: MyBooks
and its formula is: =OFFSET('Book list'!$A$1;0;0;COUNTA('Book list'!$A$1:$A$500);1)

I also made this code:
Code:
Dim BooksName As String

    For Each WS In Worksheets
        If WS.CodeName = "Books" Then
            BooksName = WS.Name
            Exit For
        End If
    Next WS

    ActiveWorkbook.Names.Add Name:="MyBooks", RefersTo:= _
    "=OFFSET('" & BooksName & "'!$A$1;0;0;COUNTA('" & BooksName & "'!$A$1:$A$500);1)"

This code gives error 1004. What is wrong with above code?

I run the code through Step Into and create Add watch and find that the formula in code have exactly the same string value as my original formula but it is still not working.

Any help?
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Changing the Sheet name by the user should change the name definition dynamically. You better check that first.
 
Upvote 0
Yes iyyi,
you are right! I did not test it, my mistake. It is changed dynamically, which is great! :)

But, what is wrong with code, how to create new named range using VBA for instance?
 
Last edited:
Upvote 0
Hi

Replace the semicolons with commas.
Code:
    ActiveWorkbook.Names.Add Name:="MyBooks", RefersTo:= _
     "=OFFSET('" & BooksName & "'!$A$1,0,0,COUNTA('" & BooksName & "'!$A$1:$A$500),1)"
NMytå
 
Upvote 0
No way!!
I thought I tried this... I read hundreds of time that this is occasionally problem with excel-semicolons and commas.
The strange is that it is comas in VBA, but when I edit range in excel it is semicolons, but it is working that is only important.

Thanks a lot NMytå and you iyyi also
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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