Sha*_*n S 0 excel vba excel-vba
我不知道这个代码有什么问题,有人可以发现错误吗?把我的错误归咎于我:
对象不支持此属性或方法.
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
Run Code Online (Sandbox Code Playgroud)
代码有几个问题
Option Explicit.这将迫使您声明变量.例如oSht未申报.Integers.您很可能在xl2007 +中遇到错误.将它们声明为LongActiveSheet/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
Run Code Online (Sandbox Code Playgroud)