2013-01-23 59 views
7

我想使自定義VBA函數只接受單元格參數。什麼是這樣做的華匯:Excel/VBA:傳遞單個單元格作爲參數

  • myCell as Cell

或:

  • myRange as Range並獲得左上細胞在默認情況下(如何?)?
+0

您可以顯示功能以及..你可以在'MyFunction的(爲ByRef了myCell爲範圍)加上'爲參數 – bonCodigo

+0

其實,好像經常發生,解決方案提出質疑後出現:'topLeftValue = myRange.Cells(1,1).Value' – user776686

+0

是您正在查找的以下答案驗證? – bonCodigo

回答

14

如果選擇了一個小區多個功能將退出:

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 
+0

+ 1給OP什麼問 –

+0

所以我可以這樣調用這個函數----> AcceptOneCell(Cells(1,1))(這會工作嗎? – BKSpurgeon

+0

是的,你可以。也可以定義單元格(Range)來自哪個表單:Sheet1.Cells(1,1)。 – InContext

7

假設你的用戶將輸入與多列和行的範圍內,你可以做以下檢查以退出功能,如果這是你的問題是什麼意思?

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 
+0

+ 1爲什麼OP問 –

4
topLeftValue = myRange.Cells(1, 1).Value 
0
numRow = myCell.Columns.Count 

numCol = myCell.Rows.Count 

numColum = myCell.Columns.Count 

numRow = myCell.Rows.Count 
相關問題