范围属性语法中的Cells属性在excel对象中不起作用,但在模块中工作

时间:2017-09-22 06:05:49

标签: vba excel-vba excel

语法,

Sheets("Working1").Range(Cells(2, 2), Cells(5, 5)).Select

此语法在Microsoft Excel对象中不起作用,但在模块中工作。为什么呢?

请任何人帮忙。

1 个答案:

答案 0 :(得分:1)

您需要先激活工作表,然后才能在工作表上选择范围。

Option Explicit

Sub SelectRange()

    'What will be better is to reference the WorkBook as well.
    With Sheets("Working1")
        'Activate the sheet before selecting the range
        .Activate
        'You need to reference the WorkBook and WorkSheet
        'Also notice the . before the cells _
                by using the with statement this references the cells in the Working1 Sheet
        .Range(.Cells(2, 2), .Cells(5, 5)).Select

    End With

End Sub

Sub SelectRange()

    'Without the with statement
    Sheets("Working1").Activate
    Sheets("Working1").Range(Sheets("Working1").Cells(2, 2), Sheets("Working1").Cells(5, 5)).Select


End Sub