gpt4 book ai didi

字符串数组上的 VBA "Type mismatch: array or user-defined type expected”

转载 作者:行者123 更新时间:2023-12-02 16:54:24 25 4
gpt4 key购买 nike

我有一个动态字符串 DMA 数组,我在全局声明它。

Dim DMAs() As String

我重新调整数组并在 CreateArrayOf 函数中为其赋值,该函数的类型为 String(),返回 String() 类型的数组

DMAs = CreateArrayOf(Sites, 2, "", False) 

Public Function CreateArrayOf( _
ByRef arrayFrom() As String, _
Optional ByVal numOfChars As Integer = 2, _
Optional ByVal filterChar As String = "", _
Optional ByVal filterCharIsInteger As Boolean = False _
) As String()

Dim i As Integer, _
j As Integer, _
strn As Variant, _
switch As Boolean, _
strArray() As String

'numOfChars 2 for DMA with no filterChar
'numOfChars 3 for W with filterChar "W"
'numOfChars 3 for A with filterChar "A"
'numofChars 2 for D with filterChar "D"

ReDim strArray(LBound(arrayFrom) To LBound(arrayFrom)) 'required in order to
'not throw error on first iteration

For i = LBound(arrayFrom) To UBound(arrayFrom) 'iterate through each site
switch = False

For Each strn In strArray 'iterate through the array to find whether the
'current site already exists
If strn = Mid(arrayFrom(i), 1, numOfChars) And Not strn = "" Then
switch = True
End If
Next strn

If switch = False Then 'if it doesn't exist add it to the array
ReDim Preserve strArray(1 To UBound(strArray) + 1)
strArray(UBound(strArray) - 1) = Mid(arrayFrom(i), 1, numOfChars)
End If
Next i

CreateArrayOf = strArray 'return the new array
End Function

当我尝试将 DMA 数组传递给另一个函数 OutputAnArray 时

Private Sub OutputAnArray(ByRef arrayToOutput() As String)

Dim i As Variant
Dim x As Integer
x = 1
For Each i In arrayToOutput
Cells(x, 6).Value = i
x = x + 1
Next i

End Sub

我收到“类型不匹配:需要数组或用户定义的类型”。在整个过程中我只搞乱了字符串数组。

如果我获取 OutputAnArray 函数的内容并将其放入我调用它的父函数中,一切都会很好。

感谢任何帮助。

最佳答案

我将所有字符串定义更改为变体

Private Sub OutputAnArray(ByRef arrayToOutput() As Variant)

罪魁祸首仍然存在,所以经过大量尝试使其编译后,我从 arrayToOutput 参数中删除了 (),它开始工作。

Private Sub OutputAnArray(ByRef arrayToOutput As Variant) 'fixed

令人困惑的是,在下面的函数定义中,arrayFrom 需要 ()。

Public Function CreateArrayOf(ByRef arrayFrom() As Variant, _ ...

我真的不明白,如果有人有任何解释,我很想听听。

关于字符串数组上的 VBA "Type mismatch: array or user-defined type expected”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30924775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com