簡體   English   中英

VBA function 作為宏運行,但使用 function 調用時出錯

[英]VBA function runs as a macro but gives error when called with function

我有一個名為 AnimeList 的 excel 表,其中列出了我已看完的所有動漫及其信息。 該表具有以下標題:

名稱、主要類型、類型 2、類型 3、評級、季節、劇集、分鍾/集、狀態。

我編寫了一些 VBA 代碼,可以計算 3 列中的不同類型以及它們存在的數量。

Function CountAndSortGenre()

Dim size As Integer: size = Range("AnimeList[Main Genre]").Rows.Count

ReDim genreExtract((size * 3) - 1) As String

Dim i As Integer: i = 0
Dim cell As Range
For Each cell In Range("AnimeList[Main Genre]")
    genreExtract(i) = cell.Value
    i = i + 1
Next
For Each cell In Range("AnimeList[Genre - 2]")
    genreExtract(i) = cell.Value
    i = i + 1
Next
For Each cell In Range("AnimeList[Genre - 3]")
    genreExtract(i) = cell.Value
    i = i + 1
Next

Dim distinctGenres As New Dictionary
Dim genre As Variant
For Each genre In genreExtract
    If distinctGenres.exists(genre) Then
        distinctGenres(genre) = distinctGenres(genre) + 1
    Else
        distinctGenres.Add genre, 1
    End If
Next

size = distinctGenres.Count
Erase genreExtract

ReDim sortedGenres(size - 1, 1) As Variant
For i = 0 To distinctGenres.Count - 1
    sortedGenres(i, 0) = distinctGenres.Keys(i)
    sortedGenres(i, 1) = distinctGenres.Items(i)
Next i

distinctGenres.RemoveAll
QuickSort sortedGenres, 0, size - 1 'This is done in a separate function

End Function

最后,我得到了我需要的東西,即排序后的流派在我的 sortedGenre 數組中計數。 但是我現在需要將 output 放到 excel 表上,這被證明是一項相當艱巨的任務。

在聲明中添加返回類型“As Variant”並在末尾添加語句 CountAndSortGenre = sortedGenres 后,我嘗試調用 function,如下所示:

=CountAndSortGenre()

但返回的數組不會溢出多個單元格。 相反,只有數組的第一個元素顯示在我輸入公式的單元格上。

我嘗試使用 Ctrl+Shift+Enter 將公式更改為:

{=CountAndSortGenre()}

但這並沒有改變 output。 它仍然是數組的第一個元素

我試着把它放在索引公式中,如下所示:

INDEX(CountAndSortGenre(), 1, 2)

試圖至少得到數組的第一個值以外的東西,但仍然只返回第一個值。

之后,我嘗試使用手動方法將值推送到單元格中,方法是刪除 As Variant 返回類型和最后的返回值並添加以下代碼:

For i = 0 To size - 1
    Application.ActiveCell.Offset(i + 1, 1) = sortedGenres(i, 0)
    Application.ActiveCell.Offset(i + 1, 2) = sortedGenres(i, 1)
Next i

這種方法在我運行代碼時有效,但是當我嘗試使用 function 時,如:

= CountAndSortGenre()

Excel 給了我循環引用警告,因此它不起作用。

我不想使用宏並將其用作 function 的原因是我希望在更新源表時更新這些值。 我不確定使用 function 是否會是動態的,但這是最好的選擇。 但現在我只想讓這個 function 開始工作。

我使用了一個數組列表,因為我懶得 go 尋找我的 QuickSort 例程; 我只為水平 output 創建了一個維度 output。

我使用范圍作為 function 的參數,因此當調用范圍中的單元格發生更改時,它會動態更新。

如果您的范圍可能在大小上發生變化,我建議使用動態命名范圍,或使用帶有結構化引用的表格,其中任何一個都可以自動調整大小。

如果需要垂直output,可以在設置function的output之前進行Transpose; 或循環成二維數組。

Option Explicit
Option Compare Text
Function CountAndSortGenre(rg As Range) As Variant()
    Dim v As Variant, w As Variant
    Dim distinctGenres As Object
    
v = rg

Set distinctGenres = CreateObject("System.Collections.ArrayList")
With distinctGenres
    For Each w In v
        If w <> "" Then
            If Not .contains(w) Then .Add w
        End If
    Next w
    .Sort
    CountAndSortGenre = .toarray
End With
        
End Function

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM