2011-12-28 146 views
0

我試圖創建一個函數,以便= processCells(A1:A10)將單元格範圍添加到每個項目並顯示單元格A11中的新數字:A20。我想在工作表中使用該功能,以便用戶可以手動選擇單元格A1:A10,因此他們可以選擇B1:B10等...而不是A1:A10將excel中的單元格範圍傳遞給vba函數

我的問題是將單元格範圍一個工作表到一個函數然後處理它們。

回答

2

下面是一個示例UDF,讓你開始

Function processCells(rng As Variant, Optional AddValue = 10) As Variant 
    Dim v As Variant 
    Dim i As Long, j As Long 
    Select Case TypeName(Application.Caller) 
     Case "Range" 
      ' Called from a Formula 
      v = rng 
      If IsArray(v) Then 
       ' Called from an Array Formula 
       For j = 1 To UBound(v, 1) 
       For i = 1 To UBound(v, 2) 
        If IsNumeric(v(j, i)) Then 
         v(j, i) = v(j, i) + AddValue 
        End If 
       Next i, j 
      Else 
       ' Called from a single Cell 
       If IsNumeric(v) Then 
        v = v + AddValue 
       End If 
      End If 
      processCells = v 
     Case Else 
      processCells = vbEmpty 
    End Select 
End Function 

要爲您介紹用它,在細胞的細胞輸入爲數組公式=processCells(A1:A10)A11:A20

相關問題