Extract and List Matching Cells

前端 未结 2 1785
南旧
南旧 2021-01-27 03:20

I\'m trying to compare two columns (A and B) containing company names, find any names that are an exact match, and list them in column C. With the code below I don\'t get an err

2条回答
  •  被撕碎了的回忆
    2021-01-27 03:49

    Try to process in memory with arrays and avoid loops when faster 'prove existence' methods are available..

    Sub matchComps()
        Dim i As long, j As long, arrA as variant, arrB as variant, arrC as variant
    
        with workSheets("Sheet1")
            arrA = .range(.cells(3, "A"), .cells(.rows.count, "A").end(xlup)).value2
            arrb = .range(.cells(3, "B"), .cells(.rows.count, "B").end(xlup)).value2
            redim arrc(1 to application.min(ubound(arra, 1) ,ubound(arrb, 1)), 1 to 1)
    
            for i= lbound(arra, 1) to ubound(arra, 1) 
                if not iserror(application.match(arra(i, 1), arrb, 0)) then
                    j=j+1
                    arrc(j,1) = arra(i, 1)
                end if
            next i
    
            .cells(3, "C").resize(ubound(arrc, 1), ubound(arrc, 2)) = arrc
        end with
    
    End Sub
    

提交回复
热议问题