我正在编写一个代码段以排除重复的单元格。因此,如果第4列中的单元格以0-9开头(全部采用文本格式)并且未出现在第8列中,我想将单元格值添加到字符串变量中。但似乎交叉方法总是在这里返回Nothing ...任何想法?
Sub getAddNum()
addnum = ""
Set rng1 = Columns(4).SpecialCells(xlCellTypeConstants)
Set rng2 = Columns(8).SpecialCells(xlCellTypeConstants)
For Each currentcell In rng1
cellValue = CStr(currentcell.Value)
If InStr("0123456789", CStr(Left(cellValue, 1))) And Intersect(currentcell, rng2) Is Nothing Then
addnum = addnum & CStr(currentcell.Value) & ", "
End If
Next
Range("I9").Value = addnum
End Sub
答案 0 :(得分:1)
正如我所评论的那样,重写这样的代码:
Sub getAddNum()
Dim addnum As String, cellValue As String
Dim rng1 As Range, rng2 As Range, currentcell As Range
addnum = ""
Set rng1 = Columns(4).SpecialCells(xlCellTypeConstants)
Set rng2 = Columns(8).SpecialCells(xlCellTypeConstants)
For Each currentcell In rng1
cellValue = CStr(currentcell.Value)
If Left(cellValue, 1) Like "[0-9]" And IsError(Application.Match(cellvalue,rng2,0)) Then
addnum = addnum & CStr(currentcell.Value) & ", "
End If
Next
Range("I9").Value = addnum
End Sub
没有经过测试,但我认为你可以得到逻辑 希望这有所帮助。