+ Reply to Thread
Results 1 to 1 of 1

Check if a Named Range exists (global scope) and tell the user about confilcting situation

Hybrid View

  1. #1
    Registered User
    Join Date
    07-19-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2013
    Posts
    36

    Check if a Named Range exists (global scope) and tell the user about confilcting situation

    I saw some information on but I need to be a bit more specific.

    I have a macros that names the headers of a region of data. It error checks if the proposed header names aren't valid names. I've attached.

    I want to check if the a named range exists in the ActiveWorkbook. I've tried the code below, as an if to check if the names exist in the the workbook. It doesn't quite work. I've attached an example sheet for reference as well.

    Any help would be much appreciate.


    Sub example()
    
    
        Dim clnames As Range
        
        
        Dim checkednames As Range
        
        
        For Each clnames In Range(ActiveCell.CurrentRegion.Cells(1, 1), ActiveCell.CurrentRegion.Cells(1, 1).End(xlToRight))
            'rCol.Value = clnames
    
    
            'I want to tell the user not to start a name with a number
            If InStr(1, clnames, "1") Then
    
    
        MsgBox clnames.Address() & " contains an invalid header name because '" & clnames & "' begins with a number. Don't begin a name with a number, include a space, use characters (&, +, etc.), or use punctuation."
    
            ElseIf InStr(1, clnames, "2") Then
    
        MsgBox clnames.Address() & " contains an invalid header name because '" & clnames & "' begins with a number. Don't begin a name with a number, include a space, use characters (&, +, etc.), or use punctuation."
    
        'you get the idea about the numbers thing
        
            ElseIf clnames = "" Then
    
                MsgBox clnames.Address() & " is a blank header. You need to make sure that your header has value. Empty cells cannot be names."
    
            ElseIf InStr(1, clnames, " ", vbTextCompare) Then
    
                MsgBox clnames.Address() & " contains an invalid header name because '" & clnames & "' contains a space. Don't begin a name with a number, include a space, use characters (&, +, etc.), or use punctuation."
            ElseIf InStr(1, clnames, ",", vbTextCompare) Then
    
                MsgBox clnames.Address() & " contains an invalid header name because '" & clnames & "' contains a comma. Don't begin a name with a number, include a space, use characters (&, +, etc.), or use punctuation."
    
            End If
        Next clnames
        'that checks if the named range had one of the identificable errors, which would prevent a name from being made, errors are skipped
        
        On Error GoTo ErrorHandler
        
        'name the header
        
        For Each checkednames In ActiveCell.CurrentRegion.Columns
           'check if the values equal existing names in the workbook
            
            With checkednames
            If .Resize(.Rows.Count - (.Rows.Count - 1)).Name = ActiveWorkbook.Name Then
            MsgBox .Resize(.Rows.Count - (.Rows.Count - 1)).Address() & " contains a Name that already exists in this WorkBook. You must provide named range a unique name."
            
            Else
            
            .Resize(.Rows.Count - 1).Offset(1).Name = "'" & .Worksheet.Name & "'!" & .Cells(1).Value
            End If
            End With
            
        Next checkednames
        
        
        ' I want to check if the values inside the headers already exists, just to let the user know
        ' The  next step is to warn the user, and to check for locally scoped and global scope,
       
        
        
    ErrorHandler:
    
        
        Resume Next
        
    
    
    End Sub

    So basically, I want to get a pop that says: hey wait, that name already exists, rename it. Eventually I want to put in an input box that allows for people to rename on the spot but thats the next step.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Named Range Scope
    By OzTrekker in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-12-2012, 02:22 AM
  2. Check if date exists in Named Range "Holiday_List"
    By carsto in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2012, 02:33 PM
  3. Check if a named range exists
    By Hein in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-14-2010, 09:16 AM
  4. Check if named range exists and ignore if it doesn't
    By lawtonl in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-14-2009, 10:05 AM
  5. [SOLVED] Check if named range exists!
    By steve_doc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-29-2006, 07:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1