我的代码在声明变量时卡在中间 - 我是 VBA 新手

问题描述

'我的代码是从打开的工作簿中的每个工作表复制数据,然后 粘贴在其主数据表中。

Sub CallData()
            
        Dim CorpFile As Worksheet,copyRg As Range,PasteRg As Range
        
        Set CorpFile = Sheets("MasterData")
        Set copyRg = ActiveSheet.UsedRange
        Set PasteRg = CorpFile.Cells(Rows.Count,1).End(xlUp).Row + 1 'it is stuck here
        
        For Each Sheet In ThisWorkbook
        
        If ActiveSheet.Name <> CorpFile Then
        
        copyRg.copy PasteRg
        
        End If
        
        Next Sheet
        
        End Sub

解决方法

Option Explicit
Sub CallData()

    Dim CorpFile As Worksheet,sheet As Worksheet
    Dim PasteRg As Range
        
    Set CorpFile = ThisWorkbook.Sheets("MasterData")
    
    For Each sheet In ThisWorkbook.Sheets
    
        If sheet.Name <> CorpFile.Name Then

            Set PasteRg = CorpFile.Cells(Rows.Count,1).End(xlUp).Offset(1)
            sheet.UsedRange.Copy PasteRg
            
        End If
    
    Next sheet
        
End Sub