Question

I am writing a snippet to exclude duplicated cells. So if there is a cell in column 4 that starts with 0-9 (all in text format) and does not appear in column 8, I would like to add the cell value to a string variable. But it seems the intersect method always return Nothing here...any idea?

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
Was it helpful?

Solution

As I've commented, re-write your code like this:

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

Not Tested, but I think you can get the logic.
Hope this somehow helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top