MrExcel's Learn Excel #676 - Recording Naming

If you like this content, please consider visiting the video on YouTube and subscribe to the MrExcel Channel to show your support!
This video has been published on Feb 19, 2009.
Paul uses the macro recorder to record the action of naming the current region. The macro recorder never gets this correct. There is a far simpler version of code to create named ranges. Episode 676 will show you how to replace the recorded code.

This blog is the video podcast companion to the book, Learn Excel 97-2007 from MrExcel. Download a new two minute video every workday to learn one of the 377 tips from the book!
maxresdefault.jpg


Transcript of the video:
Hey, welcome back to the MrExcel netcast. I'm Bill Jelen.
Today we have a question that's sent in by Paul. Paul from Derby.
Now you remember Paul.
On Friday, he gave us the great idea about how to do VLOOKUP using a named range and he sent in a question with his suggestion.
He said anytime that I use the macro recorder to record a named range, it's not doing what I want.
So basically Paul said. You know, try this go to "Tools" "Macro" "Record New Macro" and I'll call it aaaNameRange Will show up at the top of the list.
Click OK Paul says first thing I do is I select the entire current region So that's control+& to select the current region and then I define a name, so maybe "Mydata" Click OK.
All right now. We'll stop recording and I'm going to delete that define name. "Insert" "Name" "Define", and I'll delete that.
Click OK but now here's the problem.
Tomorrow Paul's data might have a larger area. All right, so we select one cell in the data. We run the macro Run and while it selects all of the data, it did not name it correctly. Unfortunately the macro recorder hard-Coded that the named range is always going to be that original area.
Which is very very frustrating.
Now Let's go take a look at this code.
"Edit" and basically what it's doing is, it's doing CurrentRegion.Select at that point.
I just want to say Selection.Name ="MyData" Far easier Syntax than whatever the Macro recorder does.
We don't have to worry about the refers to, variable or anything like that, it just simply works now.
Let's come back, and we'll test again. So this time let me delete a few cells here and I'll delete the original definition of "MyData" "Delete" Click ok All right select one cell will run the macro.
It selects all the data and already I can tell it work because the name box is showing the correct range if I would reselect and ask for my data, it has now named the entire current region.
So you know there's a few things that you have to learn and the macro recorder just don't work.
Naming ranges, it is not necessarily the best thing in the macro recorder.
It always hard codes the range that you have today by using this trick and basically changing one line of code and actually simplifying the line of code quite a bit.
Selection.Name = and then in quotes the name of the range.
It will solve that problem.
So thanks to Paul for sending in that great question and thanks to you for stopping by.
We'll see you next time for another netcast from MrExcel.
 

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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