3

I see people had this problem back in 2005 and it's still not fixed.
enter image description here

In my sheet I have these named ranges:
A1 - "name1"
B1 - "name2"
C1 - "name3"

In some other 3 cells I have these formulas:

=A1
=A1+B1
=COUNT(A1:C1)

After running Apply Names... I expect to have this:

=name1
=name1+name2
=COUNT(name1:name3)

I never get that no matter what options I choose. Moreover, usually my formulas become completely ruined...

=name1
=name1
=name1

How could I get the result I need?
I am knowledgeable in VBA, so I tried to design a solution to this problem using VBA, but the code was not working either.

I'm using Excel 2013, 32-bit (MS Office Professional Plus). OS - Windows 7 Enterprise, 64-bit.

ZygD
  • 2,459
  • 12
  • 26
  • 43
  • 1
    I'm getting the same problem as you. You're not alone! – Andi Mohr Dec 03 '15 at 13:03
  • What versions of excel are you using? OP and @AndiMohr – Raystafarian Dec 03 '15 at 13:07
  • @Raystafarian I'm on Excel 2013 (Office Pro Plus 2013) on 64bit Windows 7 Pro. – Andi Mohr Dec 03 '15 at 13:09
  • I added to my answer I cannot reproduce with excel 2007 on windows 7, this is what I have at work. I'll try to vpn home.. – Raystafarian Dec 03 '15 at 13:10
  • 2
    All right, I used 2013 w7 and added it to my answer. Same issue. 10 years ago was excel 2003 though.. @AndiMohr – Raystafarian Dec 03 '15 at 13:17
  • 1
    It seems a number have people [have](http://answers.microsoft.com/en-us/office/forum/office_2013_release-excel/when-will-the-apply-names-bug-in-excel-be-fixed/0eb23d54-fbf0-4c64-8bad-cf52bc32547f?db=5) [flagged](http://excelribbon.tips.net/T008266_Applying_Range_Names_to_Formulas.html) [this](http://www.mrexcel.com/forum/excel-questions/705730-applying-names-existing-formula.html) as a bug in Excel 2010 & 2013. One suggestion I've found is to install the [freeware Name Manager tool from Jan Karel Pieterse](http://www.jkp-ads.com/officemarketplacenm-en.asp). – Andi Mohr Dec 03 '15 at 15:22
  • 1
    @AndiMohr thank's for the link to the MS Answers site. It's also [here](https://social.msdn.microsoft.com/Forums/office/en-US/e8cb0b2e-45b1-4651-9d35-6280f9f0d370/how-do-i-get-an-acknowledgement-from-ms-of-the-bug-in-apply-names-in-excel-2010-2013?forum=exceldev) with indication it was posted to the partners site, but I don't have access to that. We may just [have to accept it](http://superuser.com/questions/611854/prevent-excel-from-clearing-copied-data-for-pasting-after-certain-operations-w) – Raystafarian Dec 03 '15 at 17:40
  • 1
    I added excel 2016 to my answer - very strange. @AndiMohr – Raystafarian Dec 04 '15 at 10:07
  • @ZygD This is the first time I'd even noticed `Apply Names` was a thing. Whenever I've had to swap cell references for named ranges like this in the past, I've used `Find and Replace`. Is there a reason this wouldn't work for you? – Andi Mohr Dec 04 '15 at 11:46
  • 1
    Added another answer with a VBA solution. It has some limitations, but it should work. @AndiMohr – Raystafarian Dec 04 '15 at 14:26
  • 1
    In my macro solution I link to my question over on [code review](http://codereview.stackexchange.com/q/112885/75587), it seems the best option for a work-around would *probably* be regex. I'm not a regex guru by any means. I think [Excellll](http://superuser.com/users/76571/excellll) has decent experience with regex (and excel), but most likely you'll find a better workaround over on [stack overflow](http://stackoverflow.com/questions/tagged/excel%20regex?mode=all). – Raystafarian Dec 06 '15 at 10:14

4 Answers4

2

I can't replicate this (with my version of excel on Windows 7) -

enter image description here

If I define your names and then create formulas

enter image description here

Then apply names

enter image description here

I get the desired result -

enter image description here

How are you defining the names? I select the cell then click on its title to the left of the formula bar and type in the name.

enter image description here


With yours -

enter image description here

apply names -

enter image description here

As shown here

enter image description here


No change without "showing formulas" -

enter image description here

apply names

enter image description here


Okay, here we go. Office 2013, windows 7

enter image description here

apply names

enter image description here

Looks like a replicated failure.


Okay, let's try Excel 2016 on OSX Yosemite

Let's define our names and formulas -

enter image description here

Good, good, let's apply our names

enter image description here

What? Alert Formula is too long

enter image description here

Now it selected my count and.. what? Alert Microsoft Excel cannot find any references to replace

enter image description here

And it's a.. partial failure?

enter image description here

Okay then, let's do this manually -

enter image description here

Strange, it doesn't highlight the range, just the two cells?

But it works?

enter image description here

Just for comparison, a regular count highlights the range -

enter image description here

Raystafarian
  • 21,583
  • 11
  • 60
  • 89
  • Try it with A1, B1 and C1 like in my example. – ZygD Dec 03 '15 at 12:47
  • @ZygD I added your example, cannot reproduce. – Raystafarian Dec 03 '15 at 12:51
  • I define names the same way as you do. I noticed you do it with *Show Formulas* enabled, and I tried the same thinking that this is how I can resolve my issue. But unfortunately I still have the same situation - `=name1` in all of the 3 cells which had formulas before *Apply Names* was used. – ZygD Dec 03 '15 at 12:55
  • I also tried using *Apply Names* with different set of Options which appear after clicking that button on the dialog. No success. How else can I help you to reproduce the issue? – ZygD Dec 03 '15 at 13:00
  • I did it without showing formulas and had no change. Are you sure your names are still defined after you apply them? – Raystafarian Dec 03 '15 at 13:03
  • Yes - names are still defined after Apply Names is used. I checked it both, on the box on left of the formula bar and in Name Manager. – ZygD Dec 03 '15 at 13:06
  • 1
    Add your version of excel and OS to your question, maybe that matters – Raystafarian Dec 03 '15 at 13:11
  • 1
    Yeah, I added my 2013 attempt and *can* replicate. – Raystafarian Dec 03 '15 at 13:18
  • 1
    Added 2016 and.. it doesn't have the *same* problem, but it doesn't work either. – Raystafarian Dec 04 '15 at 10:09
1

As was pointed out in code review, this will cause problems if, for instance, it's looking for "A1" and finds "A10" etc.

Okay, here's my attempt at a work-around. With this, your formulas must use absolute references all the time. It works on named ranges larger than 1 cell.

Please note that I'm searching usedrange - but you can narrow that down as you like by resetting srchRng.

Option Explicit
Sub FixNames()

Dim ClctNames As Variant
Set ClctNames = ActiveWorkbook.Names

Dim rngName As String
Dim rngNameLoc As String
Dim strFrmla As String

Dim c As Range
Dim n As Integer

'Define as needed
Dim srchRng As Range
Set srchRng = ActiveSheet.UsedRange

'For each name (n) in the collection
For n = 1 To ClctNames.Count

    'I'm storing the Named Range's name and address as strings to use below
    rngName = ClctNames(n).Name
    rngNameLoc = ClctNames(n).RefersToRange.Address

    '--Should I break this out into a function? If so, at what point?
    For Each c In srchRng
        'We only want to test cells with formulas
        If c.HasFormula = True Then
           'We have to check if the cell contains the current named range's address
           If InStr(1, c.Formula, rngNameLoc, vbTextCompare) <> 0 Then
              'Since these are perfect matches, no need to look for length or location, just replace
              strFrmla = Replace(c.Formula, rngNameLoc, rngName)
              c.Formula = strFrmla
           End If
        End If
    Next
Next

'No error handling should be needed

End Sub

You need to use absolute references because when I pull the named range's RefersToRange.Address it's returning a range object - not a range, so I'm setting it as a string. I guess you could write a function that removes the $ absolute references if you'd like.

that was a fun one

Raystafarian
  • 21,583
  • 11
  • 60
  • 89
  • Thanks. I value your input a lot. I have read both posts (here and in Code Review). I like that you uploaded your code there and those guys gave very precious comments (upvoted the question and the answers in CR). I have analysed your code too. To my mind, the limitations of the code are too big, so I would not choose to use it on some important workbook. As you note yourself - the case of A1 and A10. – ZygD Dec 06 '15 at 16:36
1

I can't see it mentioned, but the exact formulas you give can be the issue.

They have no absolute referencing in them. So, if you create them while cell A2, say, is selected, and Apply Names, the functionality will work as well as your version of Excel allows. For me, version 2205, that is that it will offer to only apply name1 and name3, but not name2, and will successfully do so, even to the point of the second formula, A1+B1 becoming name1+B1. But it flat will not even offer to apply name2 under any circumstance I can find or create.

HOWEVER, if you leave that cell which was selected when you created the Named Ranges, that lack of absolute referencing means their Refers to formulas change to new addresses relative to the movement from that selected cell to the newly selected cell.

So name1's Refers to might change from =A1 to, say, =H3. And now the reference in the Named Range is NOT the same as the reference in the cell that has the formula =A1. So no change occurs.

Happens to me when I set it up with the relative referencing. As soon as I change it to absolute referencing, so name1 stays =A1 rather than changing, it applies that Named Range properly. As mentioned above, it goes into the second formula to apply it to the first reference, but not the second. And in the third, it nicely applies the two different names to the portions of the formula that match them.

So to get that much functionality, be sure you have absolute references in the Named Ranges: so =$A$1, not =A1.

However, I still could not get it to apply a Named Range to multiple cell addresses taken as a unit/single entity. So it was fine changing each part of $A$1:$C$1 to name1:name3, recognizing each cell in the single range address as matching a Named Range and converting each. But it would not recognize two referenced cells that were NOT part of a single reference entity as being a single thing replaceable by a Named Range that had both of them in it. That functionality seems to be absent.

As a side note, but an important one, I would mention that if you do not select (highlight) a range, but just have a single cell selected, Excel will to the Applying over the entire sheet. In the case where the references were not absolute, you may get extraordinarily unexpected results. In the example above, of a change in selected cell before Applying changing the Name's Refers to to =H3, any cell on the worksheet that has a reference to H3 will see it changed to the Named Range.

That may be a problem in and of itself. But worse, now that the change was made and that you did not see a change where you expected to see one, you may not use UNDO to undo anything that was done. You might go to the Named Range's definition and change it to A1 again, maybe with absolute referencing this time, maybe not, and then try again. And be happy when you see it work where desired. But the 14, say, formulas that used to look to cell H3 now will look to cell A1...

So select/highlight the cells you want the Applying to occur. That will keep stray application from happening.

Finally, for formula strings (or "pieces" if one prefers) that you realize will not adjust in this process, perform a Find and Replace on the places they may be to apply your nice new Named Ranges.

Jeorje
  • 36
  • 2
1

Say we start with:

enter image description here

and we already have assigned Names to A2 and B2. In the Formulas Tab, pull-down:

Define Name > Apply Names...

enter image description here

Make sure we hi-light both names and touch OK

and we get:

enter image description here

and so the Names get applied!

Gary's Student
  • 19,266
  • 6
  • 25
  • 39
  • I tried that with your data and names. Yes it works. But please try it with my data/names if you want to see some interesting stuff going on. – ZygD May 26 '15 at 11:59
  • I'll give it a try later.................I will try to build a *macro* that can process formulas in a block of cells.................. – Gary's Student May 26 '15 at 12:03
  • Have you tried it? Aren't you convinced that the behaviour is bugged? – ZygD Jun 20 '15 at 12:02
  • @ZygD .....I have tried it...........I can't get it to work reliably............I am still looking at the macro approach... – Gary's Student Jun 20 '15 at 12:06