Consulting

Results 1 to 7 of 7

Thread: Solved: Check if cell reference

  1. #1

    Solved: Check if cell reference

    Howdy,

    I am writing a custom function and just want to do a little error trapping. I want to include an If statement that makes sure if an argument is a cell reference. If not, I will display a message box.

    Can someone help me out with just the cell reference check part?

    Thanks

  2. #2
    VBAX Contributor
    Joined
    Oct 2004
    Posts
    159
    Location
    Misunderstood the question, sorry !!!
    Last edited by Emily; 09-13-2006 at 07:27 PM.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you declare the argument as type Range you will not be able to pass anything else but a range, it will error at that point.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Dim k As Range, a As String
    a = "BBB1"
    On Error Resume Next
    Set k = Range(a)
    If Err.Number <> 0 Then MsgBox "Not a range"
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    If you don't want to set the variable type like xld suggests, you could always use the TypeName function to determine it:[vba]Function MyFunction(MyVariant As Variant) As String
    If TypeName(MyVariant) = "Range" Then
    MyFunction = "you passed a range!"
    Else
    MyFunction = "you didnt pass a range"
    End If
    End Function[/vba]An example:[vba]Sub MySub()
    MsgBox MyFunction("hi")
    MsgBox MyFunction(Range("A1"))
    End Sub[/vba]Matt

  6. #6
    xld,

    That's exactly what I did. Is there any way to trap that error and give the user some info? It just gives an error when I do it.

  7. #7
    mvidas,

    I just saw your post and I think that could be the winner the I am looking for.

    Thanks!

Posting Permissions

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