[英]What is wrong with this code that generates an error?
I don't know what is wrong with this code, could someone please spot the mistake?. 我不知道这段代码有什么问题,有人可以找出错误吗? Igives me the error: 给我这个错误:
object does not support this property or method. 对象不支持此属性或方法。
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
There are couple of problems with the code 代码有几个问题
Option Explicit
. 请使用Option Explicit
。 That will force you to declare variables. 这将迫使您声明变量。 For example oSht
is undeclared. 例如,未声明oSht
。 Integers
. 处理行时,切勿将tham声明为Integers
。 There is a huge possibility that you may get an error in xl2007+. xl2007 +中很可能会出现错误。 Declare them as Long
将它们声明为Long
ActiveSheet/Select
etc. INTERESTING READ 避免使用ActiveSheet/Select
等。 有趣的阅读 Rows.Count
. 完全限定Rows.Count
。 When working with several excel files in compatibility mode, it could result in an error if you don't fully qualify them. 在兼容模式下使用多个excel文件时,如果您不完全限定它们可能会导致错误。 Is this what you are trying? 这是您要尝试的吗?
Code: (untested) 代码:(未经测试)
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.