简体   繁体   English

在Excel Vba中选择最后8行

[英]Selecting last 8 rows in Excel Vba

I want to select the last 8 rows in x amount of rows (the amount changes each month), there is a thread already here but only for one column. 我想选择x行数的最后8行(每月的金额变化),这里有一个线程,但只有一列。 I need it for several columns and tried two different ways but both do not work due to syntax. 我需要几个列,并尝试了两种不同的方法,但由于语法,两者都不起作用。

Sheets("Sheet2").Select
LastRow = Range("D" & Rows.Count).End(xlUp).Row
Set Last8Rows =  Range("A" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8aRows = Range("B" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8bRows = Range("C" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8cRows = Range("D" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8dRows = Range("E" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8eRows = Range("F" & LastRow).Offset(-7, 0).Resize(8, 1)
Set Last8fRows = Range("G" & LastRow).Offset(-7, 0).Resize(8, 1)
LastxRows = Last8Rows + Last8aRows + Last8bRows + Last8cRows + Last8dRows + Last8eRows + Last8fRows
LastxRows.Copy

My second try 我的第二次尝试

Sheets("Sheet2").Select
LastRow = Range("D" & Rows.Count).End(xlUp).Row
Set Last8Rows =  Range("A:D" & LastRow).Offset(-7, 0).Resize(8, 1)
Last8Rows.Copy

Try the code below (there is no need to Select the sheet in order to copy it): 请尝试以下代码(无需Select表以进行复制):

Option Explicit

Sub Copy_LastEight_Rows()

Dim Sht2            As Worksheet
Dim LastRow         As Long
Dim Last8Rows       As Range

Set Sht2 = ThisWorkbook.Sheets("Sheet2")

LastRow = Sht2.Range("D" & Sht2.Rows.Count).End(xlUp).Row

' modify Column D to your need
Set Last8Rows = Sht2.Range("A" & LastRow - 7 & ":D" & LastRow)
Last8Rows.Copy

End Sub

When you are not sure of your range, ie you do not know what your last column is then try this :) It uses .Find to find the last row and last column. 如果你不确定你的范围,即你不知道你的最后一列是什么,那么试试这个:)它使用.Find来查找最后一行和最后一列。

Sub Sample()
    Dim ws As Worksheet
    Dim sRow As Long, lRow As Long, lCol As Long
    Dim LastCol As String
    Dim rng As Range

    Set ws = Sheet1 '<~~ Change as applicable

    With ws
        '~~> Find last row
        lRow = .Cells.Find(What:="*", _
                After:=.Range("A1"), _
                Lookat:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Row

        '~~> Find last column
        lCol = .Cells.Find(What:="*", _
                After:=.Range("A1"), _
                Lookat:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByColumns, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Column

        '~~> Get column letter of last column
        LastCol = Split(.Cells(, lCol).Address, "$")(1)
        sRow = lRow - 7

        Set rng = .Range("A" & sRow & ":" & LastCol & lRow)

        Debug.Print rng.Address
    End With
End Sub
Sheets("Sheet2").Select
LastRow = Range("D" & Rows.Count).End(xlUp).Row
Set Last8Rows =  Range("A" & LastRow - 7, "D" & LastRow)
Last8Rows.Copy

Using your last code, this is it: 使用您的上一个代码,就是这样:

Option Explicit

Sub Main()

    Call SelectLastRows

End Sub

Sub SelectLastRows(Optional str_start As String = "A", _
                    Optional str_end As String = "D")

    Dim l_last_row  As Long
    Dim last_8_rows As Range


    l_last_row = Range(str_start & Rows.Count).End(xlUp).Row
    Set last_8_rows = Range(str_start & l_last_row & ":" & str_end & l_last_row).Offset(-7, 0).Resize(8, 1)
    last_8_rows.Copy

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM