excel VBA:将数据从一个工作簿复制到另一个工作簿

xjreopfe  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(314)

我正在尝试编写VBA宏,以便将数据从一个工作簿复制到另一个工作簿。代码没有引发错误,但子例程在“复制”步骤退出并跳回到父子例程。如果我编写了“复制”步骤的代码,代码将按预期运行。
我已经在测试例程中测试了“复制”步骤中的代码,如果所有内容都在同一工作簿中,该代码可以正常工作。我不确定在两个工作簿之间切换时会发生什么情况。
wbk_main是在模块级别定义的。
如有帮助,敬请谅解。子程序如下:

Sub GetSheets(wbkName)

    Dim ws As Worksheet
    Dim i As Integer
    Dim wbk As Workbook
    Dim wb_Name As String

    Set wbk = Application.Workbooks(wbkName)
           
    i = 1
    For Each ws In wbk.Worksheets
        wb_Name = ws.Name
        If InStr(wb_Name, "15") Then
            MsgBox wb_Name
                wbk_main.Sheets.Add After:=wbk_main.Sheets(wbk_main.Sheets.Count)
                wbk_main.ActiveSheet.Name = wb_Name
                wbk_main.ActiveSheet.Range("A1") = "Reviewer"
                wbk_main.ActiveSheet.Range("B1") = "Criterion"
                wbk_main.ActiveSheet.Range("C1") = "Type"
                wbk_main.ActiveSheet.Range("D1") = "Level"
                wbk_main.ActiveSheet.Range("E1") = "Comment"
                wbk_main.ActiveSheet.Range("A1:E1").Font.Bold = True
                wbk.Sheets(wb_Name).Range([A39], [H39].End(xlDown)).Copy wbk_main.Sheets(wb_Name).Range("A2")
                MsgBox "Done"
        End If
        i = i + 1
    Next ws
    
End Sub

期望子例程将数据从一个工作表复制到另一个工作表。但是,子例程退出并跳回到父例程。

cu6pst1q

cu6pst1q1#

从其他工作簿导入数据

Sub ImportDataTEST()
    ImportData "Book2", ThisWorkbook
End Sub

Sub ImportData( _
        ByVal SourceWorkbookName As String, _
        ByVal DestinationWorkbook As Workbook)
    
    Const SRC_CONTAINS_CRITERION As String = "15"
    Const SRC_FIRST_ROW_RANGE As String = "A39:H39"
    Const DST_FIRST_CELL As String = "A1"
    Dim Headers(): Headers _
        = VBA.Array("Reviewer", "Criterion", "Type", "Level", "Comment")

    Dim swb As Workbook
    On Error Resume Next
        Set swb = Workbooks(SourceWorkbookName)
    On Error GoTo 0
    If swb Is Nothing Then Exit Sub ' workbook not open (doesn't exist!?)
    
    Dim sws As Worksheet, srg As Range, slCell As Range, swsName As String
    Dim dws As Worksheet, dfCell As Range
           
    For Each sws In swb.Worksheets
        swsName = sws.Name
        If InStr(swsName, SRC_CONTAINS_CRITERION) > 0 Then
            With DestinationWorkbook
                Set dws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
            End With
            With dws
                ' Suppress error if destination worksheet name is taken.
                On Error Resume Next
                    .Name = swsName
                On Error GoTo 0
                Set dfCell = .Range(DST_FIRST_CELL)
            End With
            With dfCell.Resize(, UBound(Headers) + 1)
                .Value = Headers
                .Font.Bold = True
            End With
            If sws.FilterMode Then sws.ShowAllData ' prevent 'Find' failure
            With sws.Range(SRC_FIRST_ROW_RANGE)
                Set slCell = .Resize(sws.Rows.Count - .Row + 1) _
                    .Find("*", , xlFormulas, , xlByRows, xlPrevious)
                If Not slCell Is Nothing Then
                    Set srg = .Resize(slCell.Row - .Row + 1)
                End If
            End With
            If Not srg Is Nothing Then
                srg.Copy dfCell.Offset(1)
                Set srg = Nothing
            End If
        End If
    Next sws
    
End Sub

相关问题