功能仅适用于某些子范围?

时间:2018-02-18 22:50:51

标签: vba excel-vba excel

此功能的要点是返回最常见的电影类型。

[...]

public ICommand LoadedCommand { get; private set; }

public VendorsListViewModel(IUnitOfWork unitOfWork) : base() {
    UnitOfWork = unitOfWork;
    CurrentPage = 1;
    ModelsPerPage = 25;
    LoadedCommand = new RelayCommand(this.Loaded);
}

private void Loaded()
{
    SetVendors();
}

现在我的FindMax函数如下所示:

Function MoviesByGenre(genreRng As Range) As String
Dim genreList(1 To 4) As String
Dim current As Integer
current = 1
For i = 1 To genreRng.count
    Dim found As Integer
    found = 0
    For j = 1 To current
        If genreList(j) = genreRng.Cells(i) Then
            found = 1
            Exit For
        End If
    Next j
    If found = 0 Then
        genreList(current) = genreRng.Cells(i)
        current = current + 1
    End If
Next i

Dim genreCount(1 To 4) As Integer
For i = 1 To 4
    Dim count As Integer
    count = 0
    For j = 1 To genreRng.count
        If genreRng.Cells(j) = genreList(i) Then
            count = count + 1
        End If
    Next j
    genreCount(i) = count
Next i
MoviesByGenre = FindMax(genreCount, genreList)
End Function

FindMax似乎在其他方面效果很好,但根据我用于MoviesByGenre的范围,它可能会也可能不会起作用。 (有时它会给我#VALUE!,有时它会给我实际最常见的电影类型,我不知道为什么。)我正在使用Excel 2016 for MacOS。

1 个答案:

答案 0 :(得分:0)

你的意思是那样吗

Sub Test()
    Dim a As Variant
    a = Range("A1:A7").Value

    MsgBox FindMax(a)
End Sub

Function FindMax(valueArray) As String
    Dim max As Double
    Dim i As Long

    max = valueArray(LBound(valueArray), 1)
    For i = LBound(valueArray) + 1 To UBound(valueArray)
        If valueArray(i, 1) > max Then
            max = valueArray(i, 1)
        End If
    Next i
    
    FindMax = max
End Function