[英]What is wrong with this code that generates an error?
我不知道这段代码有什么问题,有人可以找出错误吗? 给我这个错误:
对象不支持此属性或方法。
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
。 这将迫使您声明变量。 例如,未声明oSht
。 Integers
。 xl2007 +中很可能会出现错误。 将它们声明为Long
ActiveSheet/Select
等。 有趣的阅读 Rows.Count
。 在兼容模式下使用多个excel文件时,如果您不完全限定它们可能会导致错误。 这是您要尝试的吗?
代码:(未经测试)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.