438对象不支持此属性或方法vba

时间:2018-07-05 12:37:01

标签: excel vba excel-vba

下面是我的代码。所有的代码都能正常工作,但是我收到错误438对象,该行不支持此属性或方法vba。 i.offset(-7,-8).paste

Sub insert_6_rows()
Dim rActive As Range
Dim wb As Workbook

Set rActive = ActiveCell

Application.ScreenUpdating = False
Dim f As Range
Set f = Sheets("Format").Range("A1:J6")
Dim FindST As Range
Set FindST = Sheets("Driver").Range("I:I").Find(What:="Subtotal", LookIn:=xlValues)
FindST.Offset(-1, 0).EntireRow.Resize(6).Insert
f.Copy
Dim i As Range
Set i = Sheets("Driver").Range("I:I").Find(What:="Subtotal", LookIn:=xlValues)
i.Offset(-7, -8).Paste
rActive.Select
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

3 个答案:

答案 0 :(得分:0)

使用Range.Copy method的Destination参数。

Sub insert_6_rows()

    Dim rActive As Range
    Dim wb As Workbook

    Set rActive = ActiveCell

    Application.ScreenUpdating = False
    Dim f As Range, FindST As Range, i As Range

    Set f = workSheets("Format").Range("A1:J6")
    Set FindST = workSheets("Driver").Range("I:I").Find(What:="Subtotal", LookIn:=xlValues)

    FindST.Offset(-1, 0).EntireRow.Resize(6).Insert

    f.Copy Destination:=workSheets("Driver").cells(FindST.row-1, "A")

    rActive.select
    Application.ScreenUpdating = True

End Sub

答案 1 :(得分:0)

  1. 使用有意义的变量名!每个人都认为i是一个简单的计数器。例如。最好将其命名为FoundCell

  2. 如果未找到任何内容,那么您将无法.Offset来自“无”,这就是失败的原因。因此,您需要测试是否找到了某些东西。

我建议:

Dim FoundCell As Range
Set FoundCell = Sheets("Driver").Range("I:I").Find(What:="Subtotal", LookIn:=xlValues)

'check if something was found
If FoundCell is Nothing Then
    MsgBox "Subtotal not found in column I"
    Exit Sub
End If

'check if found cell.row is at least 7 rows so we can offset -7
If FoundCell.Row <= 7 Then
    MsgBox "Cannot offset -7 rows because found cell is less then 7 rows from top"
    Exit Sub
End If

SourceRange.Copy
FoundCell.Offset(-7, -8).Paste

答案 2 :(得分:0)

将“粘贴”更改为“ PasteSpecial”

i.Offset(-7, -8).PasteSpecial