选择最后一列的最后一行

时间:2014-10-12 17:46:00

标签: excel excel-vba vba

我正在尝试选择工作表中最后一列的最后一行。

Sub Paste2()
'
' Paste2 Macro
'

'
   Sheets("Macro").Select
   a = Range("B1")
   Sheets("Sheet1").Select
   ActiveSheet.Cells(2, a).End(x1Down).Offset(1, 0).Select
End Sub

变量" a"链接到单独的工作表上的CountA函数,计算具有非空单元格的行。

当我运行宏时,它会出现运行时错误1004应用程序定义或对象定义的错误。

我如何使这项工作?

2 个答案:

答案 0 :(得分:1)

两个错误:

错误1:ActiveSheet.Cells(2,1)

错误2:xlDown,而不是x1Down!

答案 1 :(得分:0)

试试这个:

Sub pfindLastNonEmptyCell()

Dim rngRange                As Range
Dim wksWorksheet            As Worksheet

'Set 'Sheet1' worksheet
Set wksWorksheet = Worksheets("Sheet1")


With wksWorksheet
    'Check for last non-empty cell
    Set rngRange = .Cells.Find("*", .Cells(1, 1), xlFormulas, xlWhole, xlByRows, xlPrevious)
End With

If Not rngRange Is Nothing Then
    'if found then activate the worksheet and select last non-empty cell
    wksWorksheet.Activate
    wksWorksheet.Cells(rngRange.Row, rngRange.Column).Select
Else
    'if not found gives message
    MsgBox "No Data in " & wksWorksheet.Name, vbCritical + vbInformation, "Error"
End If

End Sub