Excel VBA运行时错误“ 5”无效的过程调用或参数

时间:2019-07-10 12:00:33

标签: excel vba

当空单元格是表的一部分时,会发出执行代码的问题。

一切。

Private Sub Data()

Dim Answer As String
Dim Box As String

'Question to confirm

    Box = "Have you pasted the data into the tab?"

'Display MsgBox

    Answer = MsgBox(Box, vbQuestion + vbYesNo, "Data")

'Code for No press

    If Answer = vbNo Then
    MsgBox "Please paste the data in the tab before running this code"

    Else

    If Answer = vbYes Then
    MsgBox "Running code."

    Sheets("Data").Activate
    Range("D2:AO2").Copy

    Dim LR4 As Long, cell4 As Range, rng4 As Range
    LR4 = Range("D" & Rows.Count).End(xlUp).Row
    For Each cell4 In Range("D" & LR4)
        If cell4.Value <> "" Then
                Set rng4 = cell4
            Else
                Set rng4 = Union(rng4, cell4)
            End If

                Next cell4
    rng4.Offset(1, 0).PasteSpecial

    End If
    End If

End Sub

如果代码运行并且该单元格不是表的一部分,则代码将正确运行。如果该单元格是表的一部分,则会弹出运行时错误。

0 个答案:

没有答案