15

I want to make my custom VBA function accept only a single-cell argument. What is the rightway of doing it:

  • pass myCell as Cell

or:

  • pass myRange as Range and get (how?) the left-upper cell by default?
7
  • Can you show the function as well.. you can add in Myfunction(ByRef myCell as Range) as the parameter Commented Jan 23, 2013 at 12:26
  • Actually, as if often happens, solution comes up after asking the question: topLeftValue = myRange.Cells(1, 1).Value Commented Jan 23, 2013 at 12:28
  • is the following answer's validation that you are looking for? Commented Jan 23, 2013 at 12:32
  • I was searching for a swifty way for setting a default value. My above comment contains an answer that works best for me. Will add it as my own answer. Thanks anyway for the effort. But BTW: what's the purpose of ByRef - you meant to alter the param? Commented Jan 23, 2013 at 12:36
  • You don't provide the definition of topLeftValue though. It's best always to show your code instaed of a line. I missed out the default part though, if you had emphasized that you want to take first cell of any range then yes, would have been clearer. Commented Jan 23, 2013 at 12:39

5 Answers 5

25

If you select more than one cell the function will exit:

Function AcceptOneCell(rng As Range)

If (rng.Cells.Count > 1) Then
    AcceptOneCell = "Only allow 1 cell"
    Exit Function
End If

    ' your code here

End Function
Sign up to request clarification or add additional context in comments.

2 Comments

so can i call this function like this----> AcceptOneCell( Cells(1,1)) (will that work?
yes you can. Good also to define what sheet the cell (Range) is coming from: Sheet1.Cells(1,1) for example.
11

Assuming your user will enter a range with multiple columns and rows, you can do the following check to exit the function if that's what you meant in the question...

Function myFunction(ByRef myCell as Range) as SomeDataType_of_your_choice
Dim numRow as Long, numCol as Long

numRow = myCell.Columns.Count 
numCol = myCell.Rows.Count 

If numRow > 1 or numCol > 1 Then
   MsgBox "Only one cell is accepted"
   Exit Function    
Else
   '-- do other stuff you want to do here
End If
End Function

1 Comment

+ 1 for giving what the OP asked
4
topLeftValue = myRange.Cells(1, 1).Value

Comments

1

I've added the below range checks within my own functions/subs, in order to proceed even if a range of > 1 cell is passed. This forces the selection of the top-left-most cell in the case a range with multiple cells is passed to the function. This is an alternative path to the other answers, which end the current function instead.

Sub DoSomethingWithCells(StartCell As Range)
'Ensure that only the first cell of range is selected

If (StartCell.Cells.Count > 1) Then
    'Select only the first cell
    StartCell = StartCell.Cells(1, 1).Address
End If

'Do whatever you need to do here

End Sub

Comments

0
numRow = myCell.Columns.Count 

numCol = myCell.Rows.Count 

Should be

numColum = myCell.Columns.Count 

numRow = myCell.Rows.Count  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.