题
我正在编写一个代码段来排除重复的细胞。因此,如果列4中有一个单元格,那么从0-9开始(全部以文本格式),并且不会出现在第8列中,我想将单元值添加到字符串变量。但似乎相交方法总是在这里返回任何东西......任何想法?
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
. 解决方案
正如我所评论的那样,重新编写这样的代码:
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
.
没有测试,但我认为你可以得到逻辑。
希望以某种方式帮助。
不隶属于 StackOverflow