【问题标题】:Excel VBA error on searching and paste script搜索和粘贴脚本时出现 Excel VBA 错误
【发布时间】:2021-12-06 03:24:02
【问题描述】:

我正在制作 excel VBA 脚本,它将第一张表 ̣̣(Input) 上的值复制并粘贴到数据存储表 (DATA)。如果 Input sheet 上 J4 单元格的更新标志变为 1,它将搜索 DATA sheet 中包含 Input sheet K4 单元格上的值的单元格并将值粘贴到它。一半的目标已经完成,但是在编写脚本以查找并将值粘贴到具有 K4 单元格值的单元格时,它调用了

错误 1004:应用程序定义的对象定义的错误

在脚本的 PasteSpecial 行上。

我的脚本在这里:

Sub add_data()
Dim DATA As Worksheet
Dim Input As Worksheet
Dim otk As Range
Set D = ThisWorkbook.Sheets("DATA")
Set I = ThisWorkbook.Sheets("Input")
L = 1
While L = 1
    If I.Range("J4").Value = 0 Then
        I.Range("K4:UX4").copy
        lastrow = D.Range("B1")
        D.Range("A" & lastrow).PasteSpecial Paste:=xlPasteValues, _
                                                    Operation:=xlPasteSpecialOperationNone, _
                                                    SkipBlanks:=False
        L = 0
    Else

        With D.Range("A1:A10000")
            Set otk = .Find(I.Range("K4").Value, LookIn:=xlValues)
            If Not otk Is Nothing Then
                I.Range("J4").Value = 0
            Else
                I.Range("K4:UX4").copy
                D.Range(otk).PasteSpecial Paste:=xlPasteValues, _
                                                                 Operation:=xlPasteSpecialOperationNone, _
                                                                 SkipBlanks:=False
                L = 0
            End If
        End With
    End If
Wend
End Sub

【问题讨论】:

  • 需要设置范围 otk

标签: excel vba


【解决方案1】:

问题来了

Set otk = .Find(I.Range("K4").Value, LookIn:=xlValues)
If Not otk Is Nothing Then
    ' runs when otk is something
    I.Range("J4").Value = 0
Else
    ' runs when otk is nothing
    I.Range("K4:UX4").copy
    D.Range(otk).PasteSpecial Paste:=xlPasteValues, _
                                                     Operation:=xlPasteSpecialOperationNone, _
                                                     SkipBlanks:=False
    L = 0
End If

否则,当您进入Else 部分时,otk 什么都不是。 此外,otk 是一个范围对象,而不是地址,因此您需要将其用作 otk.PasteSpecial

所以你需要把If Not otk Is Nothing Then改成If otk Is Nothing Then

Set otk = .Find(I.Range("K4").Value, LookIn:=xlValues)
If otk Is Nothing Then
    ' runs when otk is nothing
    I.Range("J4").Value = 0
Else
    ' runs when otk is something
    I.Range("K4:UX4").copy
    otk.PasteSpecial Paste:=xlPasteValues, _
                     Operation:=xlPasteSpecialOperationNone, _
                     SkipBlanks:=False
    L = 0
End If

您也可以切换ElseIf 部分中的内容。

Set otk = .Find(I.Range("K4").Value, LookIn:=xlValues)
If Not otk Is Nothing Then
    ' runs when otk is something
    I.Range("K4:UX4").copy
    otk.PasteSpecial Paste:=xlPasteValues, _
                     Operation:=xlPasteSpecialOperationNone, _
                     SkipBlanks:=False
    L = 0
Else
    ' runs when otk is nothing
    I.Range("J4").Value = 0
End If

【讨论】:

  • 不是一个范围吗?所以D.Range(otk).PasteSpecial 应该是otk.PasteSpecial
  • @CDP1802 错过了那个,很好发现!我改了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 2017-11-25
  • 1970-01-01
  • 2017-07-07
  • 2018-09-27
  • 1970-01-01
相关资源
最近更新 更多