当连接的单元格与VBA匹配时,检索来自不同工作簿的数据

时间:2019-01-16 13:10:04

标签: excel vba

提出类似的问题the other day,但仍然失败...现在,我正在尝试一种在herehere上观看过的不同方法。长话短说,当我获得ID的匹配项时,我需要从源到输出检索数据。这次,我尝试将源工作簿和输出工作簿上的两个ID串联在一起,如果匹配,则将行从源复制到输出。

在代码上,我正在执行FOR语句以从源中读取每一行,并连接A1:B1中的值,依此类推,然后下一个FOR对输出执行相同的操作文件,并且具有串联的单元格criterio1criterio2 IF,存在匹配项,然后将源中的行复制到输出中。

Dim criterio1 As String
Dim criterio2 As String
For filaIndiceFuente = 2 To filaFuenteUltima
    filaIndiceFuente = filaIndiceFuente + 1
    criterio1 = planillaFuente.Range("A2" & filaIndiceFuente).Text & planillaFuente.Range("B2" & filaIndiceFuente).Text
    criterio2 = planillaDestino.Range("A2" & filaIndiceDestino).Text & planillaDestino.Range("B2" & filaIndiceDestino).Text
    For filaIndiceDestino = 2 To filaDestinoUltima
        If criterio2 = criterio1 Then
            libroFuenteSub.Sheets(1).Rows(filaIndiceFuente).Copy Destination:=libroDestino.Range("A" & filaIndiceDestino)
        End If
    Next filaIndiceDestino
Next filaIndiceFuente

运行上面的代码会在error 438内返回IF并使用msgbox完全使excel崩溃,我想这与循环有关,但我一无所知。对循环和复制/粘贴语句有帮助吗?

0 个答案:

没有答案