它应该在IsInArray没有返回True

时间:2018-02-02 22:42:03

标签: arrays excel vba excel-vba

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

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 函数:

for

很抱歉,如果代码很乱并且写得更干净,我对vba和编程整体都是全新的。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

"3" & "4" <> 34"3" & "4" = "34"Int("3" & "4") = 34。试试吧,

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

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