excel宏复制最后30行

问题描述 投票:0回答:1

我目前有一个宏,可以复制范围 A2:AJ30 并将其粘贴到下面。 有没有办法更改此设置,以便每次运行宏时,它都会占用工作表中的最后 31 行,而不是始终是第一行? 请参阅宏代码:

    If .Range("C1").Value < 1 Then
        Application.ScreenUpdating = True
        MsgBox "Please enter valid number of at least 1 copy"
        Exit Sub
    End If
    
    copies = .Range("C1").Value
    
        For i = 1 To copies
        
            lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1
            
            .Range("A" & lastRow & ":AJ" & lastRow).Interior.ColorIndex = 1
            
            lastRow = lastRow + 1
            
            .Range("A2:AJ30").Copy
            
            .Paste .Range("A" & lastRow)
    
        Next i
    
End With

Application.ScreenUpdating = True
MsgBox "Template copied " & copies & " time(s)."

结束子

我还没有尝试过任何东西,因为我不知道该怎么做

excel macros
1个回答
0
投票

这当然是可能的。如果我正确理解您想要做什么,这应该可以实现您想要的。

注意:我更喜欢在 VBA 脚本中使用行/列 (R1C1) 表示法,而不是传统的 A1 表示法。于是

Range("B5") = Cells(5, 2)

Option Explicit

Public Sub CopyLast()
On Error GoTo Err_CopyLast

    Dim i As Long              'loop iterator
    Dim copies As Long         'number of copies to make
    Dim lastDataRow As Long    'last row of data in the sheet
    Dim startCopyRow As Long   'first row to copy
    Dim endCopyRow As Long     'last row to copy
    
    Const FIRST_COL As Long = 1   'column A
    Const LAST_COL As Long = 36   'column AJ
    Const COPY_ROWS As Long = 30  'number of rows to copy
    
    'do not update the screen while working
    'this makes the code run faster
    Application.ScreenUpdating = False
    
    With ActiveSheet
        'get the number of copies to make
        copies = .Cells(1, 3).Value
        
        'verify the user selected at least one copy
        If copies < 1 Then
            MsgBox "Please enter a valid number of at least 1 copy"
            GoTo Exit_CopyLast
        End If
        
        'start from last row of column B and
        'move upwards to find last row with data
        lastDataRow = .Cells(.Rows.Count, 2).End(xlUp).Row
        
        'set the rows to be copied
        startCopyRow = lastDataRow - COPY_ROWS + 1
        endCopyRow = lastDataRow
                
        'make the copies
        For i = 1 To copies
            'make a black row to separate the existing data
            'from the copy about to be added
            With .Range(.Cells(lastDataRow + 1, FIRST_COL), _
                        .Cells(lastDataRow + 1, LAST_COL))
                .Interior.ColorIndex = 1    'black
            End With
            
            'copy the last COPY_ROWS of data
            .Range(.Cells(startCopyRow, FIRST_COL), _
                   .Cells(endCopyRow, LAST_COL)).Copy
            
            'paste the copied data
            .Paste .Cells(lastDataRow + 2, 1)
            
            'update the lastDataRow for the next iteration
            lastDataRow = lastDataRow + COPY_ROWS + 1
        Next i
    
        'deselect the copied cells
        Application.CutCopyMode = False
    
        'put the cursor in the last row of data
        .Cells(lastDataRow, 1).Activate
    End With
  
    MsgBox "Template copied " & copies & " time(s)."

Exit_CopyLast:
    'ensure screen updating gets turned back on
    'regardless of if any error occurs
    Application.ScreenUpdating = True
    Exit Sub
    
Err_CopyLast:
    'display the error information
    MsgBox Err.Number + ": " + Err.Description, _
           vbCritical + vbOKOnly, _
           "Error!"
    'exit normally
    GoTo Exit_CopyLast
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.