VBA如何将Variant数组复制到另一个数组

时间:2018-01-11 17:47:26

标签: excel-vba vba excel

我对这个子有问题:

Private Sub CommandButton1_Click()
Dim cell As Range
Dim arrayTmp(3) As Variant
Dim Occ As Long
arrayTmp = CountUnique(Selection)
'Controllo se l'utente seleziona un range che include più di 2 operatori'
If (arrayTmp(2) <> 2) Then
    MsgBox "Hai selezionato più di due operatori!" + Selection.Cells.Count
Else
    For Each cell In Selection
        If cell.Value = arrayTmp(0) Then
            cell.Value = arrayTmp(1)
        ElseIf cell.Value = arrayTmp(1) Then
            cell.Value = arrayTmp(0)
        End If
    Next cell
End If
End Sub

CountUnique(Selection)是一个返回Variant数组的函数,它是:

Option Explicit
' This function returns an array which can hold multiple values.
Public Function CountUnique(dataRange As Range) As Variant
Dim dict As Dictionary
Dim cell As Range
Set dict = New Dictionary
Dim arr(3) As Variant

For Each cell In dataRange.Cells
    If Not dict.Exists(cell.Value) Then
                dict.Add cell.Value, 0
            End If
    If (dict.Count < 3) Then
        arr(dict.Count - 1) = cell.Value
    Else
        CountUnique = arr
    End If

Next
CountUnique = arr
End Function

我的指令“arrayTmp = CountUnique(Selection)”出错了。编译器告诉我,分配矩阵是不可能的。我该如何解决?

0 个答案:

没有答案