将所有工作表的最后单元格值存储在一个工作表中

时间:2013-11-14 14:53:38

标签: excel excel-vba vba

Sub WS()
    Application.ScreenUpdating = False
    For Each Mar In ActiveWorkbook.Worksheets
        Mar.Select
        ActiveCell.Value = Selection.SpecialCells(xlCellTypeLastCell).Address(False, False)
        ActiveCell.Offset(1, 0).Select
        Next Mar
    Application.ScreenUpdating = True
End Sub

那么如何调用它的值呢? 我有10张不同的纸张,我想拉出10张不同纸张的最后一个单元格的地址。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情。你的问题有点不清楚,因为你引用了最后一个单元格Address我想拉出最后一个单元格的地址),但你也提到了Value(< em>如何调用值)。上面的代码将检索最后一个单元格的Address,但也包含检索值的代码,您只需要注释掉一行并取消注释另一行。

Sub LastCellAddresses()
    Dim Mar As Worksheet
    Dim myValues As Variant
    Dim i As Integer: i = 1

    '## Create an Array with one item for each worksheet
    ReDim myValues(1 To ActiveWorkbook.Worksheets.Count)

    '## iterate the worksheets and add values to the array
    For Each Mar In ActiveWorkbook.Worksheets

        '## Use one of these, depending on what you're trying to do.
        ' This line returns the ADDRESS
        myValues(i) = Mar.Cells.SpecialCells(xlCellTypeLastCell).Address
        ' OR this line returns the VALUES
        'myValues(i) = Mar.Cells.SpecialCells(xlCellTypeLastCell).Value
        i = i + 1
    Next Mar

    '## Print each value from the array in its own row, from
    '   the activeCell
    '   Modify as needed
    ActiveCell.Resize(UBound(myValues)).Value = Application.Transpose(myValues)

End Sub