compile error: ByRef argument type mismatch, What am I missing?
compile error: ByRef argument type mismatch, What am I missing?
我不明白问题出在哪里。
我有以下代码:
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
当我尝试 运行 时,出现以下错误:
Compile error: ByRef argument type mismatch
我认为如果我更新数组和函数的类型就可以了。
所以我将函数更改为字符串,并将 Brands() 数组也更改为字符串。然后我得到一个错误:
Compile error: Can't assign to array
在SetupForm
分行Brands() = GetTonerBrand(wsToner)
显然我遗漏了一些东西,我只是不明白它是什么。
更新
我看到这个 有相似的名字,没有帮助。
您的问题的评论中提出了很好的观点,但 none 解决了 VBA 不会神奇地将您的字典键(一个 Variant
数组)转换为一个事实String
数组。
我建议您将函数修改为 return 一个 Variant
数组。在您的调用代码中,您必须修改您的声明:
'Parent nodes
Dim Brands() As Variant
在下面的代码中,请注意删除了多余的变量,同时用合适的类型声明了剩余的变量。最后,对于 return 的值,函数的名称被分配给字典的键。
Private Function GetTonerBrand(wsSheet As Worksheet) As Variant()
Dim row As Long
Dim tonerBrands As Object
Dim tonerBrand As String
With wsSheet
row = .Cells(.Rows.Count, 2).End(xlUp).row
End With
Set tonerBrands = CreateObject("Scripting.Dictionary")
Do While row > 3
tonerBrand = CStr(wsToner.Cells(row, 2).Value)
If Not tonerBrands.Exists(tonerBrand) Then
tonerBrands.Add tonerBrand, 0
End If
row = row - 1
Loop
GetTonerBrand = tonerBrands.Keys()
End Function
关于变体数组的一个很酷的事情是你可以使用一个变体类型的变量来遍历它们,用一个简单的 For Each
.
我不明白问题出在哪里。
我有以下代码:
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
当我尝试 运行 时,出现以下错误:
Compile error: ByRef argument type mismatch
我认为如果我更新数组和函数的类型就可以了。
所以我将函数更改为字符串,并将 Brands() 数组也更改为字符串。然后我得到一个错误:
Compile error: Can't assign to array
在SetupForm
分行Brands() = GetTonerBrand(wsToner)
显然我遗漏了一些东西,我只是不明白它是什么。
更新
我看到这个
您的问题的评论中提出了很好的观点,但 none 解决了 VBA 不会神奇地将您的字典键(一个 Variant
数组)转换为一个事实String
数组。
我建议您将函数修改为 return 一个 Variant
数组。在您的调用代码中,您必须修改您的声明:
'Parent nodes
Dim Brands() As Variant
在下面的代码中,请注意删除了多余的变量,同时用合适的类型声明了剩余的变量。最后,对于 return 的值,函数的名称被分配给字典的键。
Private Function GetTonerBrand(wsSheet As Worksheet) As Variant()
Dim row As Long
Dim tonerBrands As Object
Dim tonerBrand As String
With wsSheet
row = .Cells(.Rows.Count, 2).End(xlUp).row
End With
Set tonerBrands = CreateObject("Scripting.Dictionary")
Do While row > 3
tonerBrand = CStr(wsToner.Cells(row, 2).Value)
If Not tonerBrands.Exists(tonerBrand) Then
tonerBrands.Add tonerBrand, 0
End If
row = row - 1
Loop
GetTonerBrand = tonerBrands.Keys()
End Function
关于变体数组的一个很酷的事情是你可以使用一个变体类型的变量来遍历它们,用一个简单的 For Each
.