Test if range exists in VBA

前端 未结 4 1649
滥情空心
滥情空心 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 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
    

提交回复
热议问题