【发布时间】:2017-11-15 02:29:49
【问题描述】:
我不明白问题出在哪里。
我有以下代码:
Public Sub SetupForm()
Dim wbMain As Workbook
Dim wsToner As Worksheet
Set wbMain = ActiveWorkbook
Set wsToner = wbMain.Sheets("Toner")
With DashboardForm
'Parent nodes
Dim Brands() As String
Brands() = GetTonerBrand(wsToner)
最后一行调用如下函数:
Private Function GetTonerBrand(wsSheet As Worksheet) As String
Dim col, Counter
Dim LastCol
Counter = 0
Dim LastRow
Dim Brands() As String
With wsSheet
LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
Dim RowCount
RowCount = LastRow
End With
Dim dict
Set dict = CreateObject("Scripting.Dictionary")
Do While RowCount > 3
If Not dict.exists(wsToner.Cells(RowCount, 2).Value) Then
dict.Add wsToner.Cells(RowCount, 2).Value, 0
End If
RowCount = RowCount - 1
Loop
ReDim Brands(0 To dict.Count)
Brands() = dict.keys()
GetTonerBrand Brands
End Function
当我尝试运行它时,我收到以下错误:
编译错误:ByRef 参数类型不匹配
我认为如果我更新数组和函数的类型,那么它会起作用。
所以我将函数更改为字符串,并将 Brands() 数组也更改为字符串。然后我得到一个错误:
编译错误:无法赋值给数组
在SetupForm子线上Brands() = GetTonerBrand(wsToner)
显然我错过了一些东西,我只是不明白它是什么。
更新
我看到这个other question 有一个相似的名字,它没有帮助。
【问题讨论】:
-
GetTonerBrand 的最后一行应该是
GetTonerBrand = Brands。 -
倒数第二行,应该是
GetTonerBrand = Brands -
GetTonerBrand说它返回一个字符串,但你返回一个字符串数组,所以也改为As String() -
您似乎正在尝试将字符串分配给数组 (Brands() = GetTonerBrand(wsToner))。 Brands() 是一个数组,但你的函数只是一个字符串。
标签: vba excel excel-2016