我不知道这段代码有什么问题,有人可以发现错误吗?给我一个错误:
object不支持此属性或方法。
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
答案 0 :(得分:5)
代码有几个问题
Option Explicit
。这将迫使您声明变量。例如oSht
未声明。Integers
。您很可能在xl2007 +中遇到错误。将它们声明为Long
ActiveSheet/Select
等INTERESTING READ 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