首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更正vba中的错误,然后复制粘贴结果

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

Stack Overflow用户
提问于 2019-01-20 11:23:56
回答 2查看 68关注 0票数 0

嗨,我是宏excel的新手,我试图将值从某个工作簿复制到另一个工作簿,现在我在代码中被错误困住了,我找不到解决方案,我已经在互联网上做了一些研究来寻找错误任务的解决方案,但最终没有解决方案。错误在"if“语法中,我不理解,因为我想我已经正确地编写了语法,但是错误总是出现在那里。

这是我的vba代码。

代码语言:javascript
复制
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

有什么建议吗?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-20 12:01:23

如果错误出现在行中:

If wsCopy.Range("B“&8+ d) =(”)

您可以将其替换为:

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

Stack Overflow用户

发布于 2019-01-20 11:52:28

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54275947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档