提取并列出匹配单元格 [英] Extract and List Matching Cells

查看:45
本文介绍了提取并列出匹配单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较包含公司名称的两列(A和B),查找完全匹配的任何名称,并将它们列在C列中.使用下面的代码,我没有收到错误,但是什么也没发生.如果有人能指出我正确的方向,将不胜感激.

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 error but nothing happens. If someone could point me in the right direction it would be appreciated.

Sub match()
Dim LastRow As Integer
Dim i As Integer

LastRow = Range("B" & Rows.Count).End(xlUp).Row

For i = 3 To LastRow

Set Row2Name = Sheets("Sheet1").Cells(i, 2)
Set Row1Name = Sheets("Sheet1").Cells(i, 1)
Set MatchName = Sheets("Sheet1").Cells(i, 1)

If Cells(i, 2) = Row1Name Then
Row2Name.Copy
MatchName.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End If
Next i

End Sub

推荐答案

尝试使用数组在内存中进行处理,并在有更快的证明存在"方法可用时避免循环.

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

这篇关于提取并列出匹配单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆