PC Review


Reply
Thread Tools Rate Thread

DELETE A RANGE NAME IF EXISTS

 
 
Simon
Guest
Posts: n/a
 
      13th Apr 2010
Hi
I have a macro that imports data from a text file and creates a new range
name every time.
fnd_gfm_1
I need some code to delete this range name if it exists.
Note that the 1 can change depending on how many times the macro has run, so
the code needs to delete fnd_gfm_*

Any help is much appreciated
Thanks
Simon
 
Reply With Quote
 
 
 
 
B Lynn B
Guest
Posts: n/a
 
      13th Apr 2010
Dim myName as String

For Each myName In ActiveWorkbook.Names
If Left(myName.Name, 8) = "fnd_gfm_" Then
ActiveWorkbook.Names(myName).Delete
End if
Next

adjust as needed if not active workbook...


"Simon" wrote:

> Hi
> I have a macro that imports data from a text file and creates a new range
> name every time.
> fnd_gfm_1
> I need some code to delete this range name if it exists.
> Note that the 1 can change depending on how many times the macro has run, so
> the code needs to delete fnd_gfm_*
>
> Any help is much appreciated
> Thanks
> Simon

 
Reply With Quote
 
B Lynn B
Guest
Posts: n/a
 
      13th Apr 2010
I just stole my earlier snippet from something I had working in a particular
situation that suited my own purpose. But after looking a little closer, it
would be better and more flexible like this...

Dim myName as Name
Dim wb as Workbook
Set wb = Workbooks("WorkbookToLookIn.xls") '(or whatever yours is named)

For Each myName In wb.Names
If Left(myName.Name, 8) = "fnd_gfm_" Then
MyName.Delete
End if
Next



"B Lynn B" wrote:

> Dim myName as String
>
> For Each myName In ActiveWorkbook.Names
> If Left(myName.Name, 8) = "fnd_gfm_" Then
> ActiveWorkbook.Names(myName).Delete
> End if
> Next
>
> adjust as needed if not active workbook...
>
>
> "Simon" wrote:
>
> > Hi
> > I have a macro that imports data from a text file and creates a new range
> > name every time.
> > fnd_gfm_1
> > I need some code to delete this range name if it exists.
> > Note that the 1 can change depending on how many times the macro has run, so
> > the code needs to delete fnd_gfm_*
> >
> > Any help is much appreciated
> > Thanks
> > Simon

 
Reply With Quote
 
Simon
Guest
Posts: n/a
 
      14th Apr 2010
Thanks Lynn
I have tried this but it doesnt seem to work.
The code runs OK, but names are still there.

"B Lynn B" wrote:

> I just stole my earlier snippet from something I had working in a particular
> situation that suited my own purpose. But after looking a little closer, it
> would be better and more flexible like this...
>
> Dim myName as Name
> Dim wb as Workbook
> Set wb = Workbooks("WorkbookToLookIn.xls") '(or whatever yours is named)
>
> For Each myName In wb.Names
> If Left(myName.Name, 8) = "fnd_gfm_" Then
> MyName.Delete
> End if
> Next
>
>
>
> "B Lynn B" wrote:
>
> > Dim myName as String
> >
> > For Each myName In ActiveWorkbook.Names
> > If Left(myName.Name, 8) = "fnd_gfm_" Then
> > ActiveWorkbook.Names(myName).Delete
> > End if
> > Next
> >
> > adjust as needed if not active workbook...
> >
> >
> > "Simon" wrote:
> >
> > > Hi
> > > I have a macro that imports data from a text file and creates a new range
> > > name every time.
> > > fnd_gfm_1
> > > I need some code to delete this range name if it exists.
> > > Note that the 1 can change depending on how many times the macro has run, so
> > > the code needs to delete fnd_gfm_*
> > >
> > > Any help is much appreciated
> > > Thanks
> > > Simon

 
Reply With Quote
 
ozgrid.com
Guest
Posts: n/a
 
      14th Apr 2010
Step through this code with F8 and hover over "nName.Name" to ensure the
names matches the pattern


Sub DeleteNames()
Dim nName As Name

For Each nName In ThisWorkbook.Name
If nName.Name Like "fnd_gfm*" _
Then nName.Delete
Next nName
End Sub



--
Regards
Dave Hawley
www.ozgrid.com
"Simon" <(E-Mail Removed)> wrote in message
news:B0BA3020-CFC6-44B4-88B3-(E-Mail Removed)...
> Thanks Lynn
> I have tried this but it doesnt seem to work.
> The code runs OK, but names are still there.
>
> "B Lynn B" wrote:
>
>> I just stole my earlier snippet from something I had working in a
>> particular
>> situation that suited my own purpose. But after looking a little closer,
>> it
>> would be better and more flexible like this...
>>
>> Dim myName as Name
>> Dim wb as Workbook
>> Set wb = Workbooks("WorkbookToLookIn.xls") '(or whatever yours is
>> named)
>>
>> For Each myName In wb.Names
>> If Left(myName.Name, 8) = "fnd_gfm_" Then
>> MyName.Delete
>> End if
>> Next
>>
>>
>>
>> "B Lynn B" wrote:
>>
>> > Dim myName as String
>> >
>> > For Each myName In ActiveWorkbook.Names
>> > If Left(myName.Name, 8) = "fnd_gfm_" Then
>> > ActiveWorkbook.Names(myName).Delete
>> > End if
>> > Next
>> >
>> > adjust as needed if not active workbook...
>> >
>> >
>> > "Simon" wrote:
>> >
>> > > Hi
>> > > I have a macro that imports data from a text file and creates a new
>> > > range
>> > > name every time.
>> > > fnd_gfm_1
>> > > I need some code to delete this range name if it exists.
>> > > Note that the 1 can change depending on how many times the macro has
>> > > run, so
>> > > the code needs to delete fnd_gfm_*
>> > >
>> > > Any help is much appreciated
>> > > Thanks
>> > > Simon


 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      14th Apr 2010
Are your actual range Names all in lower case as you showed? The tests Simon
(and Dave) posted are case sensitive tests. These can be made case
insensitive if you need them to be, but I thought I would just check first.

--
Rick (MVP - Excel)



"Simon" <(E-Mail Removed)> wrote in message
news:B0BA3020-CFC6-44B4-88B3-(E-Mail Removed)...
> Thanks Lynn
> I have tried this but it doesnt seem to work.
> The code runs OK, but names are still there.
>
> "B Lynn B" wrote:
>
>> I just stole my earlier snippet from something I had working in a
>> particular
>> situation that suited my own purpose. But after looking a little closer,
>> it
>> would be better and more flexible like this...
>>
>> Dim myName as Name
>> Dim wb as Workbook
>> Set wb = Workbooks("WorkbookToLookIn.xls") '(or whatever yours is
>> named)
>>
>> For Each myName In wb.Names
>> If Left(myName.Name, 8) = "fnd_gfm_" Then
>> MyName.Delete
>> End if
>> Next
>>
>>
>>
>> "B Lynn B" wrote:
>>
>> > Dim myName as String
>> >
>> > For Each myName In ActiveWorkbook.Names
>> > If Left(myName.Name, 8) = "fnd_gfm_" Then
>> > ActiveWorkbook.Names(myName).Delete
>> > End if
>> > Next
>> >
>> > adjust as needed if not active workbook...
>> >
>> >
>> > "Simon" wrote:
>> >
>> > > Hi
>> > > I have a macro that imports data from a text file and creates a new
>> > > range
>> > > name every time.
>> > > fnd_gfm_1
>> > > I need some code to delete this range name if it exists.
>> > > Note that the 1 can change depending on how many times the macro has
>> > > run, so
>> > > the code needs to delete fnd_gfm_*
>> > >
>> > > Any help is much appreciated
>> > > Thanks
>> > > Simon


 
Reply With Quote
 
Simon
Guest
Posts: n/a
 
      14th Apr 2010
Still not working for me.
ThisWorkbook.Name created a compiler error so I changed to .Names, is that
correct?

Hovering over shows nName.Name ="_xlfn.IFERROR"

The range names are lower case and I am using v2007.

Any thoughts
Thanks in advance
Simon

"ozgrid.com" wrote:

> Step through this code with F8 and hover over "nName.Name" to ensure the
> names matches the pattern
>
>
> Sub DeleteNames()
> Dim nName As Name
>
> For Each nName In ThisWorkbook.Name
> If nName.Name Like "fnd_gfm*" _
> Then nName.Delete
> Next nName
> End Sub
>
>
>
> --
> Regards
> Dave Hawley
> www.ozgrid.com
> "Simon" <(E-Mail Removed)> wrote in message
> news:B0BA3020-CFC6-44B4-88B3-(E-Mail Removed)...
> > Thanks Lynn
> > I have tried this but it doesnt seem to work.
> > The code runs OK, but names are still there.
> >
> > "B Lynn B" wrote:
> >
> >> I just stole my earlier snippet from something I had working in a
> >> particular
> >> situation that suited my own purpose. But after looking a little closer,
> >> it
> >> would be better and more flexible like this...
> >>
> >> Dim myName as Name
> >> Dim wb as Workbook
> >> Set wb = Workbooks("WorkbookToLookIn.xls") '(or whatever yours is
> >> named)
> >>
> >> For Each myName In wb.Names
> >> If Left(myName.Name, 8) = "fnd_gfm_" Then
> >> MyName.Delete
> >> End if
> >> Next
> >>
> >>
> >>
> >> "B Lynn B" wrote:
> >>
> >> > Dim myName as String
> >> >
> >> > For Each myName In ActiveWorkbook.Names
> >> > If Left(myName.Name, 8) = "fnd_gfm_" Then
> >> > ActiveWorkbook.Names(myName).Delete
> >> > End if
> >> > Next
> >> >
> >> > adjust as needed if not active workbook...
> >> >
> >> >
> >> > "Simon" wrote:
> >> >
> >> > > Hi
> >> > > I have a macro that imports data from a text file and creates a new
> >> > > range
> >> > > name every time.
> >> > > fnd_gfm_1
> >> > > I need some code to delete this range name if it exists.
> >> > > Note that the 1 can change depending on how many times the macro has
> >> > > run, so
> >> > > the code needs to delete fnd_gfm_*
> >> > >
> >> > > Any help is much appreciated
> >> > > Thanks
> >> > > Simon

>

 
Reply With Quote
 
Simon
Guest
Posts: n/a
 
      14th Apr 2010
When I run further through it, hovering over shows
nName.Name="Report!fnd_gfm_11"
I tried editing the code to include the worksheeet name Report! but the
range names are still there.


"Simon" wrote:

> Thanks Lynn
> I have tried this but it doesnt seem to work.
> The code runs OK, but names are still there.
>
> "B Lynn B" wrote:
>
> > I just stole my earlier snippet from something I had working in a particular
> > situation that suited my own purpose. But after looking a little closer, it
> > would be better and more flexible like this...
> >
> > Dim myName as Name
> > Dim wb as Workbook
> > Set wb = Workbooks("WorkbookToLookIn.xls") '(or whatever yours is named)
> >
> > For Each myName In wb.Names
> > If Left(myName.Name, 8) = "fnd_gfm_" Then
> > MyName.Delete
> > End if
> > Next
> >
> >
> >
> > "B Lynn B" wrote:
> >
> > > Dim myName as String
> > >
> > > For Each myName In ActiveWorkbook.Names
> > > If Left(myName.Name, 8) = "fnd_gfm_" Then
> > > ActiveWorkbook.Names(myName).Delete
> > > End if
> > > Next
> > >
> > > adjust as needed if not active workbook...
> > >
> > >
> > > "Simon" wrote:
> > >
> > > > Hi
> > > > I have a macro that imports data from a text file and creates a new range
> > > > name every time.
> > > > fnd_gfm_1
> > > > I need some code to delete this range name if it exists.
> > > > Note that the 1 can change depending on how many times the macro has run, so
> > > > the code needs to delete fnd_gfm_*
> > > >
> > > > Any help is much appreciated
> > > > Thanks
> > > > Simon

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      14th Apr 2010
Give this modification of Dave's (ozgrid.com) code a try...

Sub DeleteNames()
Dim nName As Name
For Each nName In ThisWorkbook.Name
If nName.Name Like "*fnd_gfm*" Then nName.Delete
Next nName
End Sub

--
Rick (MVP - Excel)



"Simon" <(E-Mail Removed)> wrote in message
news:4B284942-42A6-4C75-8DEF-(E-Mail Removed)...
> When I run further through it, hovering over shows
> nName.Name="Report!fnd_gfm_11"
> I tried editing the code to include the worksheeet name Report! but the
> range names are still there.
>
>
> "Simon" wrote:
>
>> Thanks Lynn
>> I have tried this but it doesnt seem to work.
>> The code runs OK, but names are still there.
>>
>> "B Lynn B" wrote:
>>
>> > I just stole my earlier snippet from something I had working in a
>> > particular
>> > situation that suited my own purpose. But after looking a little
>> > closer, it
>> > would be better and more flexible like this...
>> >
>> > Dim myName as Name
>> > Dim wb as Workbook
>> > Set wb = Workbooks("WorkbookToLookIn.xls") '(or whatever yours is
>> > named)
>> >
>> > For Each myName In wb.Names
>> > If Left(myName.Name, 8) = "fnd_gfm_" Then
>> > MyName.Delete
>> > End if
>> > Next
>> >
>> >
>> >
>> > "B Lynn B" wrote:
>> >
>> > > Dim myName as String
>> > >
>> > > For Each myName In ActiveWorkbook.Names
>> > > If Left(myName.Name, 8) = "fnd_gfm_" Then
>> > > ActiveWorkbook.Names(myName).Delete
>> > > End if
>> > > Next
>> > >
>> > > adjust as needed if not active workbook...
>> > >
>> > >
>> > > "Simon" wrote:
>> > >
>> > > > Hi
>> > > > I have a macro that imports data from a text file and creates a new
>> > > > range
>> > > > name every time.
>> > > > fnd_gfm_1
>> > > > I need some code to delete this range name if it exists.
>> > > > Note that the 1 can change depending on how many times the macro
>> > > > has run, so
>> > > > the code needs to delete fnd_gfm_*
>> > > >
>> > > > Any help is much appreciated
>> > > > Thanks
>> > > > Simon


 
Reply With Quote
 
Simon
Guest
Posts: n/a
 
      15th Apr 2010
Yay it works!
Thanks you guys are great!

"Rick Rothstein" wrote:

> Are your actual range Names all in lower case as you showed? The tests Simon
> (and Dave) posted are case sensitive tests. These can be made case
> insensitive if you need them to be, but I thought I would just check first.
>
> --
> Rick (MVP - Excel)
>
>
>
> "Simon" <(E-Mail Removed)> wrote in message
> news:B0BA3020-CFC6-44B4-88B3-(E-Mail Removed)...
> > Thanks Lynn
> > I have tried this but it doesnt seem to work.
> > The code runs OK, but names are still there.
> >
> > "B Lynn B" wrote:
> >
> >> I just stole my earlier snippet from something I had working in a
> >> particular
> >> situation that suited my own purpose. But after looking a little closer,
> >> it
> >> would be better and more flexible like this...
> >>
> >> Dim myName as Name
> >> Dim wb as Workbook
> >> Set wb = Workbooks("WorkbookToLookIn.xls") '(or whatever yours is
> >> named)
> >>
> >> For Each myName In wb.Names
> >> If Left(myName.Name, 8) = "fnd_gfm_" Then
> >> MyName.Delete
> >> End if
> >> Next
> >>
> >>
> >>
> >> "B Lynn B" wrote:
> >>
> >> > Dim myName as String
> >> >
> >> > For Each myName In ActiveWorkbook.Names
> >> > If Left(myName.Name, 8) = "fnd_gfm_" Then
> >> > ActiveWorkbook.Names(myName).Delete
> >> > End if
> >> > Next
> >> >
> >> > adjust as needed if not active workbook...
> >> >
> >> >
> >> > "Simon" wrote:
> >> >
> >> > > Hi
> >> > > I have a macro that imports data from a text file and creates a new
> >> > > range
> >> > > name every time.
> >> > > fnd_gfm_1
> >> > > I need some code to delete this range name if it exists.
> >> > > Note that the 1 can change depending on how many times the macro has
> >> > > run, so
> >> > > the code needs to delete fnd_gfm_*
> >> > >
> >> > > Any help is much appreciated
> >> > > Thanks
> >> > > Simon

>
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if Range EXISTS KIM W Microsoft Excel Programming 3 24th May 2009 09:34 PM
Don't add to range if row already exists sbitaxi@gmail.com Microsoft Excel Programming 15 4th Sep 2008 09:16 PM
If a value exists in a range =?Utf-8?B?amVmZmJlcnQ=?= Microsoft Excel Programming 2 13th Feb 2007 01:40 PM
See if a Range Name exists? plh Microsoft Excel Programming 3 30th Apr 2006 10:25 PM
how to tell if a named range exists =?Utf-8?B?R2l4eGVyX0pfOTc=?= Microsoft Excel Programming 2 1st Jun 2005 07:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:16 AM.