这个代码产生错误是什么错误?
我不知道这个代码有什么问题,请问有人能发现这个错误吗? 给我的错误:
对象不支持此属性或方法。
Sub copyrow2() Dim Lastro As Integer Dim nLastro As Integer Dim Rng As Range nLastro = ActiveSheet.Cells(Rows.Count, 10).End(xlUp).Row Lastro = ActiveSheet.Cells(Rows.Count, 9).End(xlUp).Row + 1 If Lastro < nLastro Then With oSht = ActiveSheet Set Rng = oSht.Range("J" & Lastro & ":" & "k" & nLastro) Rng.Copy oSht.Range("H" & Lastro).Select ActiveSheet.Paste End With End If
代码有几个问题
- 请使用
Option Explicit
。 这将迫使你声明variables。 例如oSht
未申报。 - 在使用行时,不要将tham声明为
Integers
。 有很大的可能性,你可能会在xl2007 +中得到一个错误。 宣布他们为Long
- 避免使用
ActiveSheet/Select
等有趣的阅读 - 完全符合
Rows.Count
。 在兼容模式下处理多个excel文件时,如果没有完全限定它们,可能会导致错误。
这是你正在尝试?
代码:(未经testing)
Option Explicit Sub copyrow2() Dim oSht As Worksheet Dim Lastro As Long, nLastro As Long Dim Rng As Range '~~> Change this to the relevant sheet Set oSht = ThisWorkbook.Sheets("Sheet1") With oSht nLastro = .Cells(.Rows.Count, 10).End(xlUp).Row Lastro = .Cells(.Rows.Count, 9).End(xlUp).Row + 1 If Lastro < nLastro Then Set Rng = .Range("J" & Lastro & ":" & "k" & nLastro) Rng.Copy .Range("H" & Lastro) End If End With End Sub