2013-10-22 26 views
0

我不知道这段代码有什么问题,有人可以找出错误吗? Igives我的错误:产生错误的代码有什么问题?

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 
+0

你可以把休息在你的代码的开始,直到你的错误步。 –

+3

我已经提到它,但它重复:[请接受一些答案](http://meta.stackoverflow.com/help/someone-answers)。 –

+0

你已经提出了9个问题,但没有接受任何答案......为什么不呢? –

回答

5

有几个问题与代码

  1. 请使用Option Explicit。这将迫使你声明变量。例如oSht未申报。
  2. 使用行时,请勿将其声明为Integers。在xl2007 +中可能会出现错误的可能性很大。声明为Long
  3. 避免使用ActiveSheet/SelectINTERESTING READ
  4. 完全限定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 
+0

不客气:) –

+0

我们应该对这里发布的所有问题做一个强制性的“使用Option Explicit”警报...... – enderland

相关问题