[英]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.