Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何将工作表从一个工作簿复制到另一个工作簿_Excel_Vba - Fatal编程技术网

Excel 如何将工作表从一个工作簿复制到另一个工作簿

Excel 如何将工作表从一个工作簿复制到另一个工作簿,excel,vba,Excel,Vba,我想将通过文件路径打开的工作簿中的一张工作表复制到另一个包含我正在运行的宏的工作簿中 我遇到的问题是,每当我复制和粘贴文件时,宏都会创建一个新工作簿,并将数据粘贴到该工作簿的第一个工作表中。我在第二个工作簿中定义了一个特定的工作表来粘贴代码,因此我不确定为什么粘贴的目标是随机工作簿 Public filepath As String Sub FileOpenDialogBox() 'Display a Dialog Box that allows to select a single fi

我想将通过文件路径打开的工作簿中的一张工作表复制到另一个包含我正在运行的宏的工作簿中

我遇到的问题是,每当我复制和粘贴文件时,宏都会创建一个新工作簿,并将数据粘贴到该工作簿的第一个工作表中。我在第二个工作簿中定义了一个特定的工作表来粘贴代码,因此我不确定为什么粘贴的目标是随机工作簿


Public filepath As String

Sub FileOpenDialogBox()

'Display a Dialog Box that allows to select a single file.
'The path for the file picked will be stored in fullpath variable
  With Application.FileDialog(msoFileDialogFilePicker)
        'Makes sure the user can select only one file
        .AllowMultiSelect = False
        'Filter to just the following types of files to narrow down selection options
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
        'Show the dialog box
        .Show

        'Store in fullpath variable
        fullPath = .SelectedItems.Item(1)
    End With
    filepath = fullPath
    'It's a good idea to still check if the file type selected is accurate.
    'Quit the procedure if the user didn't select the type of file we need.
    If InStr(fullPath, ".xls") = 0 Then
        Exit Sub
    End If

    'Open the file selected by the user
    'Workbooks.Open fullpath

End Sub

Sub CopySheet()
    'Module 1 FilePath import as Variable
    MsgBox filepath

    Dim spo_book As Workbook
    Dim target_book As Workbook
    Set spo_book = ActiveWorkbook
    Set target_book = Workbooks.Open(filepath)

    Dim dst_sheet As Worksheet
    Dim target_sheet As Worksheet
    Set dst_sheet = spo_book.Sheets("SPO Data")
    Set target_sheet = target_book.Sheets("Untimed Parts")
    target_sheet.Copy
    dst_sheet.Paste

End Sub


预期结果是复制粘贴将通过my FileDialog从所选工作簿复制到名为“SPO数据”我将其设置为变量
dst_sheet
,我认为这可能是一个范围问题,我试图将其放入一个范围,但它表示数据不匹配,因此我返回到我的工作表粘贴中。

如何做有点老派:

    sub sample()

    Application.ScreenUpdating = false
    Sheet1="Willy"
    Sheet2="Wilma"

    for row1=20 to 500
    for col= 30 to 3300

    Sheets(Sheet1).Cells(row1, col1).Value=Sheets(Sheet2).Cells(row2, col2).Value
    Sheets(Sheet1).Cells(row1, col1).Formula=Sheets(Sheet2).Cells(row2, col2).Formula
    Sheets(Sheet1).Cells(row1, col1).Comment=Sheets(Sheet2).Cells(row2, col2).Comment

    next
    next
    Application.ScreenUpdating = True
    end sub
因为有人问过如何处理一些表格:)


@Urderboy我见过,但我见过很多人用路径定义两个工作簿的案例,但是这个宏将被分发给其他人,而定义路径并不是一个可行的解决方案,因为不是所有人都能够编辑一个宏使其为他们工作。我试着只定义我想从中复制的书,但即使这样也不起作用。如何做到这一点在这里也有很好的记录。您的整个问题(分段)都存在于此。搜索问题的各个组成部分。移动工作表与对话选择器无关-单独搜索这些主题并将解决方案组合在一起。如果不这样做,你实际上是在要求别人为你做你的工作项目。可能不是最漂亮的,但获取路径,移动工作表,然后另存为。。。如果不在源上保存更改,则移动实际上是复制。。。看,我已经编辑了这个问题,使其更具描述性,并对其进行了细化,使其更加狭窄。我在某些领域取得了一些进展,所以这个问题应该是关于我在工作簿之间复制和粘贴的全部问题。这看起来不错,但如果我完全理解这一点,这对将一张图纸复制到另一张图纸更有效。我理解这种形式的复制,但我的问题是关于工作簿到工作簿的工作表复制。然后你只需要看看打开的工作簿。这是一样的。作为主对象,位于所有工作簿的顶部。我加了一个样品。
  Sub ws_all()
  Dim wb As Workbook

For Each wb In Application.Workbooks
    Debug.Print wb.Name

    For Each ws In wb.Sheets

        Debug.Print ws.Name

    Next
Next

Debug.Print Application.Workbooks; ("Workbookname").Sheets  ("Sheetname").Name

End Sub