2015-12-14 59 views
1

我的Excel工作表行單元格包含的數據類型的混合組合 - 數字,阿爾法和錯誤(例如,#REF!)。我想將這個範圍傳遞給一個函數,該函數僅查找最大數量並忽略alpha和錯誤。我的代碼:返回最大價值混合數據類型的範圍

Public Function getRangeMax(passedRange As range) As Single 
'validate entries in passed range as numbers or skip 
'return max from validated numbers 

Dim i As Integer, arryLength As Integer 
Dim arry() 

getRangeMax = 0 

arry() = passedRange 
arryLength = UBound(arry) - LBound(arry) 

For i = 0 To arryLength 
    On Error Resume Next 
    If arry(i).value > getRangeMax Then 
     getRangeMax = arry(i).value 
    End If 
Next i 

'getRangeMax = Application.Max(passedRange) 

End Function 

函數調用是:= getRangeMax(C35:I35)。該特定範圍的單元格中的數據爲:dB,456.00,#REF !, 12.我希望代碼忽略dB和#REF!條目並返回最大數量,在本例中爲456.00。相反,該函數返回0.00。我究竟做錯了什麼?

+0

檢查的元素。不要認爲它持有你的想法。可能希望爲'passedRange'中的每個c'存儲到數組中。 – findwindow

+0

將範圍存儲到內存中的數組後,它是一個多維數組。在你的情況下,它會'arry(1到UBound(arry),1到1)'。 – PatricK

+0

@PatricK如果從單個列傳遞一個範圍,則會是正確的。 OP是通過'C35:I35'所以第二個維度變化 –

回答

1

你的代碼中包含多個錯誤,所有的隱藏你總是主動On Error Resume Next

這裏的重構你的代碼,修正錯誤,並用另一種類型檢查

Public Function getRangeMax(passedRange As Range) As Double 
'validate entries in passed range as numbers or skip 
'return max from validated numbers 

    Dim i As Long, j As Long 
    Dim item As Variant 
    Dim arry() 

    getRangeMax = 0 
    arry() = passedRange 

    For i = 1 To UBound(arry, 1) 
    For j = 1 To UBound(arry, 2) 
     item = arry(i, j) 
     If VarType(item) = vbDouble Then 
      If item > getRangeMax Then 
       getRangeMax = item 
      End If 
     End If 
    Next j, i 
End Function 

注:
使用VarType要測試要處理的有效項目,將導致任何單元格爲Text,但看起來像數字將被忽略。
使用IsNummeric來測試有效的項目過程將導致是文本,但樣子數字轉換爲數字,幷包含在測試的任何細胞。

+0

謝謝,克里斯。奇蹟般有效。 – jmaz

1

試試這個:

Public Function Rng_Max_Get(rTrg As Range) As Double 
Dim aTrg As Variant, vItm As Variant, blTrg As Boolean 
    aTrg = rTrg.Value2 
    For Each vItm In aTrg 
     If IsNumeric(vItm) Then 
      If vItm <> Empty Then 
       If blTrg Then 
        If vItm > Rng_Max_Get Then Rng_Max_Get = vItm 
       Else 
        blTrg = True 
        Rng_Max_Get = vItm 
    End If: End If: End If: Next 
End Function 
0

既然你輸入一個矩形範圍,你可以使用.Rows.Count.Columns.Count而不是從UBound函數獲得。試試這個:您的陣列的

Option Explicit 

Function getRangeMax(passedRange As Range) As Double 
    'validate entries in passed range as numbers or skip 
    'return max from validated numbers 

    Dim lRows As Long, lCols As Long, lR As Long, lC As Long 
    Dim uValue As Double, uMax As Double 
    Dim arry() As Variant 

    arry = passedRange 
    lRows = passedRange.Rows.Count 
    lCols = passedRange.Columns.Count 
    uMax = 0 
    For lR = 1 To lRows 
     For lC = 1 To lCols 
      If IsNumeric(arry(lR, lC)) Then 
       uValue = CDbl(arry(lR, lC)) 
       If uValue > uMax Then uMax = uValue 
      End If 
     Next 
    Next 
    getRangeMax = uMax 
End Function 
+0

使用'UBound'比顯著快'Range.Rows/Columns.Count' –

+0

@PatricK,作品,感謝 – jmaz