Test if range exists in VBA

前端 未结 4 1635
滥情空心
滥情空心 2020-12-17 17:14

I have a dynamically defined named range in my excel ss that grabs data out of a table based on a start date and an end date like this

=OFFSET(Time!$A$1,IFER         


        
相关标签:
4条回答
  • 2020-12-17 17:38

    You can replicate the match in your VBA to count before using the range how many rows you would have, or you can use error handling:

    On Error Resume Next
    
    Debug.Print range("DateRangeData").Rows.Count
    
    If Err = 1004 Then
        MsgBox "Range Empty"
        Exit Sub
    Else
        MsgBox "Range full"
    End If
    
    Err.Clear
    On Error GoTo 0
    
    0 讨论(0)
  • 2020-12-17 17:45

    Here is a function I knocked up to return whether a named range exists. It might help you out.

    Function RangeExists(R As String) As Boolean
        Dim Test As Range
        On Error Resume Next
        Set Test = ActiveSheet.Range(R)
        RangeExists = Err.Number = 0
    End Function
    
    0 讨论(0)
  • 2020-12-17 17:59

    Depending on the application you're doing, it's good to consider using a Dictionary. They're especially useful when you wanna check whether something exists. Take this example:

    Dim dictNames as Scripting.Dictionary
    
    Sub CheckRangeWithDictionary()
    
        Dim nm As Name
    
        'Initially, check whether names dictionary has already been created
        If Not dictNames Is Nothing Then
            'if so, dictNames is set to nothing
            Set dictNames = Nothing
        End If
    
        'Set to new dictionary and set compare mode to text
        Set dictNames = New Scripting.Dictionary
        dictNames.CompareMode = TextCompare
    
        'For each Named Range
        For Each nm In ThisWorkbook.Names
            'Check if it refers to an existing cell (bad references point to "#REF!" errors)
            If Not (Strings.Right(nm.RefersTo, 5) = "#REF!") Then
                'Only in that case, create a Dictionary entry
                'The key will be the name of the range and the item will be the address, worksheet included
                dictNames(nm.Name) = nm.RefersTo
            End If
        Next
    
        'You now have a dictionary of valid named ranges that can be checked
    
    End Sub
    

    Within your main procedure, all you need to do is do an existence check before using the range

    Sub CopyRange_MyRange()
    
        CheckRangeWithDictionary
    
        If dictNames.exists("MyRange") then
            Sheets(1).Range("MyRange").Copy
        end if
    
    End Sub
    

    While loading the dictionary may look a little longer, it's extremely fast to process and search. It also becomes much simpler to check whether any named range referring to a valid address exists, without using error handlers in this simple application.

    Please note that when using names at sheet level rather than workbook level, it is necessary to use more elaborate keys to guarantee uniqueness. From the way the dictionary was created, if a key is repeated, the item value is overwritten. That can be avoided by using the same Exists method as a check in the key creation statement. If you need a good reference on how to use dictionaries, use this one.

    Good luck!

    0 讨论(0)
  • 2020-12-17 18:03

    This is another approach. It has the advantage to take the container and the name you want to test. That means you can test either Sheets Names or Workbook Names for example.

    Like this:

    If NamedRangeExists(ActiveSheet.Names, "Date") Then
        ...
    Else
    ...
    End If
    

    or

    If NamedRangeExists(ActiveWorkbook.Names, "Date") Then
       ...
    Else
       ...
    End If
    

    Public Function NamedRangeExists(ByRef Container As Object, item As String) As Boolean
    
    
    Dim obj As Object
    Dim value As Variant
    
    On Error GoTo NamedRangeExistsError:
    
        value = Container(item)
        If Not InStr(1, CStr(value), "#REF!") > 0 Then
            NamedRangeExists = True
        End If
        Exit Function
    
    Exit Function
    
    NamedRangeExistsError:
        NamedRangeExists = False
    End Function
    
    0 讨论(0)
提交回复
热议问题