【问题标题】:Select the range from first cell used to last cell used in column选择从第一个单元格到列中使用的最后一个单元格的范围
【发布时间】:2019-01-29 10:17:44
【问题描述】:

我有一个宏,它选择我在列 M 中使用的第一个单元格(变量是 Firstrow)。此宏检测列 M 中使用的最后一个单元格(变量是 lastrow)。

我希望我的宏从 M 列中使用的第一个单元格到 M 列中使用的最后一个单元格进行选择。

我试过了:

范围(Firsttrow & "M:M" & lastrow)

但我收到一条错误消息

“编译错误:无效使用属性”

Sub Selectfromfirstrowusedtolastrowusedused()
Dim lastrow, Firstrow As Long
lastrow = ActiveSheet.Range("M" & Rows.Count).End(xlUp).Row
Firstrow = Range("M1").End(xlDown).Row
Debug.Print Firstrow
Debug.Print lastrow
Range (Firsttrow & "M:M" & lastrow)
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    你需要

    Range("M" & Firsttrow & ":M" & lastrow).Select

    因为它是使用 Range 的列然后是行。另一种选择是

    Range(cells(firstrow,"M"),cells(lastrow,"M")).Select

    并不是说你很少需要select 任何东西。

    还有,在这一行

    Dim lastrow, Firstrow As Long

    lastrow 被声明为一个变体,所以这样做更好

    Dim lastrow As Long, Firstrow As Long

    【讨论】:

      【解决方案2】:

      正确的做法是

      Range("M" & Firsttrow & ":M" & lastrow")

      【讨论】:

        猜你喜欢
        • 2016-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-14
        • 2018-11-25
        相关资源
        最近更新 更多