如何将从其他工作簿复制的工作表粘贴到现有工作表中?

时间:2016-07-20 17:47:58

标签: excel vba excel-vba

我发现从工作簿中复制现有工作表有点困难,让我们称之为,#34; WB_RAW"并将其粘贴到另一个工作簿中的现有工作表中。到目前为止,我有下一个代码,我从另一个帖子的答案得到。此代码成功复制了工作表,但它在工作簿中创建了一个新的工作表,我们称之为“#FinalText”###########################################而不是将信息粘贴到现有的工作簿中。

    Sub ImportSheet()
    Dim sImportFile As String, sFile As String
    Dim sThisBk As Workbook
    Dim vfilename As Variant
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Set sThisBk = ActiveWorkbook
    sImportFile = Application.GetOpenFilename( _
    FileFilter:="Microsoft Excel Workbooks, *.xls; *.xlsx", Title:="Open Workbook")
    If sImportFile = "False" Then
        MsgBox "No File Selected!"
        Exit Sub

    Else
        vfilename = Split(sImportFile, "\")
        sFile = vfilename(UBound(vfilename))
        Application.Workbooks.Open Filename:=sImportFile

        Set wbBk = Workbooks(sFile)
        With wbBk
            If SheetExists("MTM Datos") Then
                Set wsSht = .Sheets("MTM Datos")
                wsSht.Copy before:=sThisBk.Sheets("B012")
                ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
            Else
                MsgBox "There is no sheet with name :MTM Datos in:" & vbCr & .Name
            End If
            wbBk.Close SaveChanges:=False
        End With
    End If
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
Private Function SheetExists(sWSName As String) As Boolean
    Dim ws As Worksheet
    On Error Resume Next
    Set ws = Worksheets(sWSName)
    If Not ws Is Nothing Then SheetExists = True
End Function

请帮助我,自从我最后一次使用VBA以来已经有一段时间了,所以我不记得如何使用它

1 个答案:

答案 0 :(得分:0)

在此行中,您将复制包含数据的完整工作表 wsSht.Copy before:=sThisBk.Sheets("Bimbo12")

将其更改为

wsSht.Cells.Copy sThisBk.Sheets("Bimbo12").Cells(1,1)
Application.CutCopyMode=False