如何更正vba中的错误,然后vlookup复制粘贴结果

时间:2019-01-20 11:23:56

标签: excel vba

嗨,我是宏excel的新手,我试图将值从某个工作簿复制到另一个工作簿,现在我陷入了代码错误,并且我找不到解决方案,我已经在互联网上进行了一些研究以找到错误任务的解决方案,但最终没有解决方案。错误出现在我不理解的“ if”语法中,因为我猜我已经正确编写了语法,但是错误总是出现在这里。

这是我的vba代码。

Sub copy()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim wsCopyT As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim a As Long
Dim b As Long



    Set wsCopy = Workbooks("DNWID0162019011613-DOWID190115AM.xls").Worksheets("DNOZA0012015110201")
    Set wsDest = Workbooks("macro template.xlsm").Worksheets("Sheet1")
    Set wsCopyT = Workbooks("macro template.xlsm").Worksheets("Template")
    Set wsDestI = Workbooks("macro template.xlsm").Worksheets("Stock on Hand")

    lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row

    lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row


    a = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
    b = wsDestI.Cells(wsCopy.Rows.Count, "G").End(xlUp).Row

For i = 1 To (a)

If wsCopy.Cells("E" & 9 + i).Value = "" Then
    For s = 4 To b
        If wsCopy.Cells("B" & 9 + i).Value = wsDestI.Cells("G" & s).Value Then
            wsDestI.Cells("G" & s).copy
                wsDest.Cells("S" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
            wsDestI.Cells("C" & s).copy
                wsDest.Cells("M" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
            wsDestI.Cells("M" & s).copy
                wsDest.Cells("P" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
        End If
    Next s
Else: wsCopy.Cells("E" & 9 + i).copy
        wsDest.Cells("M" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
    wsCopy.Cells("K" & 9 + i).copy
        wsDest.Cells("P" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
End If
Next i
wsDest.Range("M2:M" & lDestLastRow).Select
Dim max_baris As Long

max_baris = wsDest.Range("M2").End(xlDown).Row

For d = 2 To (max_baris)
    wsCopy.Cells("E2").copy
        wsDest.Cells("B" & d).PasteSpecial Paste:=xlPasteValues
    wsCopy.Cells("E3").copy
        wsDest.Cells("AB" & d).PasteSpecial Paste:=xlPasteValues
    wsCopy.Cells("E6").copy
        wsDest.Cells("AC" & d).PasteSpecial Paste:=xlPasteValues

    'wsDest.Range("L" & i + 1) = i
    If wsCopy.Range("B" & 8 + d) = ("") Then
        wsDest.Range("S" & d) = ("0")
        Else
        wsCopy.Range("B" & 9 + d).copy
            wsDest.Range("S" & d).PasteSpecial Paste:=xlPasteValues
        End If
    wsCopyT.Cells("A2").copy wsDest.Cells("A" & d)
    wsCopyT.Cells("C2").copy wsDest.Cells("C" & d)
    wsCopyT.Cells("J2").copy wsDest.Cells("J" & d)
    wsCopyT.Cells("J2").copy wsDest.Cells("K" & d)
    wsCopyT.Cells("Q2").copy wsDest.Cells("Q" & d)
    wsCopyT.Cells("R2").copy wsDest.Cells("R" & d)
    wsCopyT.Cells("AE2").copy wsDest.Cells("AE" & d)
    wsCopyT.Cells("AG2").copy wsDest.Cells("AG" & d)
    wsCopyT.Cells("AI2").copy wsDest.Cells("AI" & d)

    Next d

End Sub
有什么建议吗?谢谢

2 个答案:

答案 0 :(得分:0)

If wsCopy.Range("B" & 8 + d) = ("") Then应该是If wsCopy.Range("B" & 8 + d).Value = "" Then甚至更好的If wsCopy.Range("B" & 8 + d).Value = vbNullString Then

答案 1 :(得分:0)

如果错误出现在行中:

如果wsCopy.Range(“ B”&8 + d)=(“”)

您可以将其替换为:

  • 如果IsEmpty(wsCopy.Range(“ B”&8 + d).Value)= True或
  • 如果wsCopy.Range(“ B”&8 + d).Value =“”