gpt4 book ai didi

excel - "invalid qualifier"数组错误。包含

转载 作者:行者123 更新时间:2023-12-04 21:54:07 24 4
gpt4 key购买 nike

我父亲想要一个 Excel 中的宏,但对于这种问题,他需要 Visual Basic。我决定帮助他,但我从未编写过 Visual Basic 代码,所以我只是将互联网论坛和 mnsd 的代码放在一起,但后来我遇到了这个问题,我不知道如何解决它。

    Dim strArray() As String
Dim TotalRows As Long
Dim i As Long

TotalRows = Rows(Rows.Count).End(xlUp).Row
ReDim strArray(1 To TotalRows)

For i = 2 To TotalRows
If Not strArray.Contains(Cells(i, 1).Value) Then
strArray(i) = Cells(i, 1).Value
End If
Next

这只是代码的一部分,但这是错误。
它显示一个错误,说

"Invalid qualifier"



和亮点 strArraystrArray.Contains(Cells... .我解决不了,所以在这里问。我认为有一个非常简单的解决方案,但我无法在网上找到它。

谢谢指教
托马斯

最佳答案

变量 strArray 是字符串类型的普通数组,而不是 List 或其他对象,因此它没有“包含”方法,您必须执行以下操作:

Dim strArray() As String
Dim TotalRows As Long
Dim i As Long

TotalRows = Rows(Rows.Count).End(xlUp).Row
ReDim strArray(1 To TotalRows)

For i = 2 To TotalRows
Dim x As Long
Dim contains As Boolean
contains = False
For x = LBound(strArray) To UBound(strArray)
If strArray(x) = Cells(i, 1).Value Then
contains = True
End If
Next
If Not contains Then
strArray(i) = Cells(i, 1).Value
End If
Next

请注意,Lbound 和 Ubound 将获得数组的上限和下限,因为您重新调整了数组,所以数组的上限和下限会发生变化。您也可以只对 TotalRows 使用 1,因为您“知道”数组的大小,但是由于我不知道您的实际代码有多复杂,因此我将 Lbaund 和 Ubound 包括在内,以防您在实际代码中需要它们。

关于excel - "invalid qualifier"数组错误。包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48194581/

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