对象'_Global'的方法'Range'失败错误

时间:2013-09-20 04:59:11

标签: excel-vba vba excel

1.Dim destbook As Workbook
2.Dim destsheet As Worksheet
3.Set destbook = ActiveWorkbook
4.Set destsheet = destbook.Sheets(1)
5.Dim ct As Integer
6.destsheet.Range("C1048576").Select
7.ct = Selection.End(xlUp).Row
8.Windows("Book1.xlsm").Activate
9.Sheets("Sheet1").Activate
10.Range(Cells(ct, 1)).Offset(1, 0).Select

这里在第10行,我收到一条错误,说“对象'_Global'的方法'范围'失败了”。 你可以帮忙吗?? ...

3 个答案:

答案 0 :(得分:0)

尝试以下代码。避免在代码中使用Activate,Select,Activeworkbook以获得更好的结果。

Sub test()

    Dim destbook As Workbook
    Dim destsheet As Worksheet

    Set destbook = ThisWorkbook
    Set destsheet = destbook.Sheets(1)

    Dim ct As Integer
    With destsheet
        ct = .Range("C" & .Rows.Count).End(xlUp).Row
    End With

    Windows("Book1.xlsm").Activate
    With Sheets("Sheet1")
        .Select
        .Range(.Cells(ct, 1)).Offset(1, 0).Select
    End With

End Sub

答案 1 :(得分:0)

您只想在第一个工作表中选择C列最后一个单元格下面的单元格:

Sub SO_18909094()
    Dim destbook As Workbook
    Dim destsheet As Worksheet

    Set destbook = ActiveWorkbook
    Set destsheet = destbook.Sheets(1)

    destsheet.Range("C" & Rows.Count).End(xlUp).Offset(1, 0).Select

    Set destbook = Nothing
    Set destsheet = Nothing
End Sub

答案 2 :(得分:0)

Cells(ct,1)是指坐标(ct,1)处单元格的 value ,实际上不是有效的单元格地址(除非您这样做,否则!),并且因此Range会引发异常。

您只需放下Range即可。

Cells(ct, 1).Offset(1, 0).Select