excel 选择性粘贴中断VBA模块

xzv2uavs  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(146)

我一直试图找出为什么我不能粘贴值只在这个VBA模块。有人能发现我的问题吗?我已经尝试了许多不同的组合,但仍然得到编译错误

Sub Merge_Sheets()

'Set Master sheet for consolidation
Set mtr = Worksheets("Master")

Set wb = ThisWorkbook

'loop through all sheets
For Each ws In wb.Worksheets
     'except the master sheet from looping
     If ws.Name <> "Master" Then
        ws.Activate
        Range("A5:P20").Copy _
        mtr.Range("A" & mtr.Cells(Rows.Count, 1).End(xlUp).Row + 1).PasteSpecial xlPasteValues
            End If
Next ws

Worksheets("Master").Activate

End Sub

有没有人对这个问题有什么想法?提前感谢!

db2dz4w8

db2dz4w81#

附加所有工作表中的范围值

Option Explicit

Sub AppendRangeValues()

    Const SRC_RANGE As String = "A5:P20"

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim dws As Worksheet: Set dws = wb.Sheets("Master")
    Dim dfCell As Range
    Set dfCell = dws.Cells(dws.Rows.Count, "A").End(xlUp).Offset(1)
    
    Dim drg As Range, rCount As Long
    
    With dws.Range(SRC_RANGE)
        rCount = .Rows.Count
        Set drg = dfCell.Resize(rCount, .Columns.Count)
    End With
    
    Dim sws As Worksheet
    
    For Each sws In wb.Worksheets
        If Not sws Is dws Then
            drg.Value = sws.Range(SRC_RANGE).Value
            Set drg = drg.Offset(rCount)
        End If
    Next sws
    
    MsgBox "Range values appended.", vbInformation
    
End Sub

相关问题