user1283776
user1283776

Reputation: 21814

Text if range exists Excel VBA

I want to test if a range exists to be able to create the following pattern:

if not exists(r) then
   MsgBox("Range is missing")
end if

Function exists(r as range) as boolean

End function

Here is an example of a range that I would like to test if it exists or not

Call RangeExists(lob.ListColumns("Leverera utt").DataBodyRange)

How can I do this?

Upvotes: 0

Views: 2492

Answers (2)

user4720266
user4720266

Reputation: 21

Avoiding the label required in ON ERROR GOTO is also possible Function RangeExists(rngName As String) As Boolean On Error Resume Next RangeExists = Range(rngName).Column And (Err.Number = 0) Debug.Print "RangeExists= " & RangeExists & " " & rngName End Function

Upvotes: 1

manu
manu

Reputation: 942

You could do it this way:

Sub CheckRange()

    Dim myRange As Variant

    myRange = InputBox("Enter your name of your range")

           If RangeExists(CStr(myRange)) Then
                MsgBox "True"
           Else
                MsgBox "No"
           End If

    End Sub

And the function:

   Function RangeExists(s As String) As Boolean
        On Error GoTo No
        RangeExists = Range(s).Count > 0
    No:
    End Function

Upvotes: 1

Related Questions