gpt4 book ai didi

arrays - IsInArray 在应该返回 True 时没有返回

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

我正在使用 IsInArray 函数来检查数组中是否存在单元格地址(行、列)。出于某种原因,即使数组包含它不匹配的值。例如,如果我的数组是这样的:

18, 812, 84, 34, 412, 87, 74

OldRow & OldCol到达 34 , 函数 IsInArray不匹配并执行 Else .

下面是我用来尝试实现此目的的代码示例:
    Set c = .Find(1, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
cellAddress = c.Address
OldRow = Range(cellAddress).Row
OldCol = Range(cellAddress).Column

If IsInArray("OldRow & OldCol", mappedcells) = False Then
oldmappingrow = Application.Match(OldRow, Worksheets(1).Range("r3:r16"), 0)
If Not IsError(oldmappingrow) Then
OldRowMapped = Worksheets(1).Range("r3:r16").Cells(oldmappingrow).Offset(, 1).Value
End If

oldmappingcol = Application.Match(OldCol, Worksheets(1).Range("r3:r16"), 0)
If Not IsError(oldmappingcol) Then
OldColMapped = Worksheets(1).Range("r3:r16").Cells(oldmappingcol).Offset(, 1).Value
End If

If OldCol > OldRow Then
NewCol = WorksheetFunction.Max(OldRowMapped, OldColMapped)
NewRow = WorksheetFunction.Min(OldRowMapped, OldColMapped)
Else
NewRow = WorksheetFunction.Max(OldRowMapped, OldColMapped)
NewCol = WorksheetFunction.Min(OldRowMapped, OldColMapped)
End If

.Cells(NewRow, NewCol) = .Cells(OldRow, OldCol).Value
.Cells(OldRow, OldCol).Value = "0"

ReDim Preserve mappedcells(UBound(mappedcells) + 1) 'Add next array element
mappedcells(UBound(mappedcells)) = NewRow & NewCol 'Assign the array element

Set c = .FindNext(c)
Debug.Print (OldRow & OldCol & " moved to " & NewRow & NewCol)
Else
Set c = .FindNext(c)
End If
Loop While Not c Is Nothing And c.Address <> firstAddress
End If

IsInArray我正在使用的功能:
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function

对不起,如果代码很乱并且可以写得更干净,我对 vba 和整个编程都是全新的。任何帮助将非常感激!

最佳答案

"3" & "4" <> 34但是 "3" & "4" = "34"Int("3" & "4") = 34 .试试看,

 If not IsInArray(clng(OldRow & OldCol), mappedcells) Then

正如 Scott Craner 的评论中所述下面,检查您的代码,并确保您将数字与数字或文本与文本进行比较,而不是将数字与看起来像数字的文本进行比较。

关于arrays - IsInArray 在应该返回 True 时没有返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591928/

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