【问题标题】:438 object doesn't support this property or method vba438 对象不支持此属性或方法 vba
【发布时间】:2018-07-05 12:37:00
【问题描述】:

下面是我的代码。所有代码都有效,但我得到错误 438 object does not support this property or method vba in this line。 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

【问题讨论】:

  • 我在 i.Offset(-7, -8).PasteSpecial 之前将 f.copy 向下移动了 3 行。现在可以使用了

标签: vba excel


【解决方案1】:

使用 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 更改为 -7,现在它在所需范围内粘贴,但 J 列似乎没有向下移动
【解决方案2】:
  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

【讨论】:

  • 您的代码帮助我找到了问题。现在我首先查找 FoundCell 然后复制和粘贴特殊。谢谢
【解决方案3】:

将粘贴更改为 PasteSpecial

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多