ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   if statement (https://www.excelbanter.com/excel-programming/391120-if-statement.html)

BorisS

if statement
 
Need an if statement to check if a certain named data range (call it
"testrange") exists or not? I am going to create it if it is not there, and
if it is, I need to delete it. So have to check, otherwise will get run
error.

Thx.
--
Boris

Bob Phillips

if statement
 
Public Function NameExists(val As String)
Dim nme
On Error Resume Next
nme = ActiveWorkbook.Names(val)
On Error GoTo 0
NameExists = (Not IsEmpty(nme))
End Function


If NameExists("xyz" Then
...

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"BorisS" wrote in message
...
Need an if statement to check if a certain named data range (call it
"testrange") exists or not? I am going to create it if it is not there,
and
if it is, I need to delete it. So have to check, otherwise will get run
error.

Thx.
--
Boris




JE McGimpsey

if statement
 
One way:

Public Sub ToggleTestRange()
Const sNAME As String = "testrange"
Dim nm As Name
With ActiveWorkbook
On Error Resume Next
Set nm = .Names(sNAME)
On Error GoTo 0
If nm Is Nothing Then
.Names.Add Name:=sNAME, RefersTo:="some value"
Else
nm.Delete
End If
End With
End Sub


Or if you just want to create the name, but delete any existing copy
before creating the new one:

Public Sub MakeTestRange()
Const sNAME As String = "testrange"
With Application
On Error Resume Next
.Names(sNAME).Delete
On Error GoTo 0
.Names.Add Name:=sNAME, RefersTo:="some value"
End With
End Sub


In article ,
BorisS wrote:

Need an if statement to check if a certain named data range (call it
"testrange") exists or not? I am going to create it if it is not there, and
if it is, I need to delete it. So have to check, otherwise will get run
error.

Thx.



All times are GMT +1. The time now is 03:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com