大家。 我的目标是获取一个弹出窗口,从不同的工作簿中提取数据,并从拉出的工作簿中的第一个工作表中复制整个工作表,并将其粘贴到原始工作簿的sheet2中。我需要最后一行的帮助。 它似乎不起作用,因为我是VBA编程的新手。 此外,是否可以打开.xlsx以及.xls文件类型? 还有一件事,是否可以复制/粘贴而无需打开不同的文件?比如禁用事件启动器..
Option Explicit
Sub test()
Dim wb As Workbook, wb2 As Workbook
Dim ws As Worksheet
Dim vFile As Variant
'Set source workbook
Set wb = ActiveWorkbook
'Open the target workbook
vFile = Application.GetOpenFilename("Excel-files,*.xlsx", _
1, "Select One File To Open", , False)
'if the user didn't select a file, exit sub
If TypeName(vFile) = "Boolean" Then Exit Sub
Workbooks.Open vFile
'Set targetworkbook
Set wb2 = ActiveWorkbook
'For instance, copy data from a range in the first workbook to another range in the other workbook
' wb.Worksheets(1).Cells.Copy _
' Destination:=newworksheet.Cells
wb.Worksheets(2).Range("C3:D4").Value = wb2.Worksheets(1).Range("A1:B2").Value
End Sub
答案 0 :(得分:0)
我怀疑在这种情况下宏录制器是否会有任何帮助。
是否可以打开.xlsx以及.xls文件类型
是。使用通配符
vFile = Application.GetOpenFilename("Excel-files,*.xls*", 1, _
"Select One File To Open", , False)
是否可以复制/粘贴而无需打开其他文件?
没有。但是,您可以从已关闭的文件中检索值。您可能希望看到THIS
我需要最后一行的帮助。它似乎不起作用,因为我是VBA编程的新手。
我没看到最后一行有什么问题。但是我会做一些改变,如下所示。确保您使用正确的工作簿和工作表。如果您仍然收到错误,请发布错误消息。
Sub test()
Dim wb As Workbook, wb2 As Workbook
Dim ws As Worksheet
Dim vFile As Variant
'Set source workbook
Set wb = ActiveWorkbook '<~~ You might want to use ThisWorkbook instead
'Open the target workbook
vFile = Application.GetOpenFilename("Excel-files,*.xlsx", _
1, "Select One File To Open", , False)
If vFile = False Then Exit Sub
'Set targetworkbook
Set wb2 = Workbooks.Open(vFile)
wb.Worksheets(2).Range("C3:D4").Value = wb2.Worksheets(1).Range("A1:B2").Value
End Sub