简体   繁体   English

Excel VBA将内容从一个工作表复制到另一个工作簿工作表

[英]Excel VBA copy content from one Sheet into other Workbook sheet

I want to be able to select a workbook and then copy the content from that workbook (sheet 1) into my current active workbook where I run the macro. 我希望能够选择一个工作簿,然后将内容从该工作簿(工作表1)复制到运行宏的当前活动工作簿中。 I've been looking at some answers here on StackOverflow to similar questions and got the following code (see below). 我一直在这里寻找关于类似问题的StackOverflow上的一些答案,并获得了以下代码(请参见下文)。

The selection of a file is currently working fine, but when I run the macro it throws an error 文件的选择当前工作正常,但是当我运行宏时会引发错误

Runtime error "438": Object does not support that method or property` 运行时错误“ 438”:对象不支持该方法或属性`

(please note, that the error comes in my native language and is just translated by me) (请注意,错误来自我的母语,由我翻译)

Sadly no object is marked that he relates to, so I can't really make out what problem he has. 遗憾的是,没有标记出与他有关的任何对象,因此我无法真正确定他有什么问题。 Yet, I guess it is a problem with the PasteSpecial in the last line of function GetTemplateData , but that code should be alright (what is it supposed to do? Save the data into the first sheet of the give workbook activeWorkbook ) and pass the reference back go GeneratedValues -routine. 但是,我想这是函数GetTemplateData的最后一行中的PasteSpecial存在问题,但是该代码应该没问题(应该怎么做?将数据保存到给定工作簿activeWorkbook的第一张表中)并传递引用回去GeneratedValues例程。

Option Explicit

Private Sub GenerateValues()
    'Application.ScreenUpdating = False
    'Application.DisplayAlerts = False
    Dim activeWorkbook As Workbook
    Dim activeWorksheet As Worksheet

    Set activeWorkbook = Application.activeWorkbook
    Set activeWorksheet = GetTemplateData(activeWorkbook)

    activeWorkbook.Save
End Sub


'Get The Template Data
Private Function GetTemplateData(activeWorkbook As Workbook) As Worksheet

    Dim templateWorkbook As Workbook

    'Grab the Template Worksheet
    Set templateWorkbook = UseFileDialogOpen

    'Select all Content
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Copy

    'activeWorkbook.Sheets(activeWorkbook.Sheets.Count).Range("A1", Cells.End(xlDown) & Cells.End(xlRight)).PasteSpecial xlPasteValues
    activeWorkbook.Sheets(1).Range("A1", Cells.End(xlDown) & Cells.End(xlRight)).PasteSpecial xlPasteValues

End Function

'From https://docs.microsoft.com/de-de/office/vba/api/excel.application.filedialog
'Select the Workbook containing the Exported Template-Stories by User Selection
Function UseFileDialogOpen() As Workbook

    Dim lngCount As Long
    Dim filePath As String

    Dim templateBook As Workbook

    ' Open the file dialog
    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = False
        .Show

        Set templateBook = Workbooks.Open(.SelectedItems(1))

        ' Display paths of each file selected
        'For lngCount = 1 To .SelectedItems.Count
        '    MsgBox .SelectedItems(lngCount)
        'Next lngCount

    End With

    templateBook
End Function

I believe all of your problems originate here: 我相信您所有的问题都源于此:

Private Sub GenerateValues()
    'Application.ScreenUpdating = False
    'Application.DisplayAlerts = False
    Dim activeWorkbook As Workbook
    Dim activeWorksheet As Worksheet

    Set activeWorkbook = Application.activeWorkbook
    Set activeWorksheet = GetTemplateData(activeWorkbook)

    activeWorkbook.Save
End Sub

ActiveWorkbook is a defined "variable" in VBA, so it is confused as to why you are trying to reassign it. ActiveWorkbook是VBA中定义的“变量”,因此对于您为何尝试重新分配它感到困惑。 Try using a different variable name instead. 尝试改用其他变量名。

Note: although ActiveWorksheet is not a defined variable in VBA, it is close in name to ActiveSheet, so I would also change that variable name to something different just so to not confuse you when writing future code. 注意:尽管ActiveWorksheet不是VBA中定义的变量,但它的名称与ActiveSheet十分接近,因此我也将该变量名称更改为其他名称,以免在编写以后的代码时造成混淆。

You could try something similar to this: 您可以尝试类似以下操作:

Sub CopyContentsFromOtherWorkbook()

    Dim wb As Workbook
    Dim twb As Workbook

    filePath = "C:\File.xlsx"

    Set wb = Workbooks.Open(filePath)
    wb.Sheets(1).Range("A1:Z10000").Copy

    Set twb = ThisWorkbook
    twb.Sheets(1).Range("C1").PasteSpecial xlPasteValues

    wb.Close
    twb.Save

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA在给定多个条件的情况下从一张工作表复制到其他工作簿 - Excel VBA Copy from one sheet to other workbook given multiple criteria VBA将数据从一个工作簿表复制到另一工作簿表 - VBA copy data from one workbook sheet to another workbook sheet Excel VBA-将范围从一个工作表粘贴复制到工作簿中的某些工作表之后的所有工作表 - Excel VBA - Copy range from one sheet paste to all sheets after certain sheet in workbook EXCEL VBA:将工作表中的工作表复制到其他位置的工作簿中 - EXCEL VBA: Copy Sheet from a workbook to another workbook in different location excel vba按一定顺序从一张纸复制到另一张纸 - excel vba copy from one sheet to an other in a certain order 试图将特定范围从工作簿中的许多工作表复制到另一个工作簿中的一个工作表 vba excel? - Trying to copy specific range from many sheets in workbook to one sheet in another workbook vba excel? Excel工作表:将数据从一个工作簿复制到另一个工作簿 - Excel sheet: Copy data from one workbook to another workbook 将数据从一个工作簿复制到另一个工作簿表 - Copy data from one workbook to the other workbook sheet 将行从一张纸复制到另一张纸,然后在7列后使用Excel中的vba转到下一行 - Copy rows from one sheet into other sheet and after 7 columns go to next row using vba in excel Excel VBA-如何使用不同的子例程将行从工作簿的行复制到另一个工作簿的工作表 - Excel VBA - How to copy row by row from a sheet of workbook to another Workbook's sheet using different subroutine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM