尝试将xlsb工作表复制到另一个工作表

我想将整个工作表复制到另一个(特定工作表)工作簿。 源工作表是以.xlsb格式。 非常感谢你提前

好的,我修改了我的答案,使其尽可能dynamic。 下面看到的代码的第一部分实现了以下内容:

浏览源表单需要复制到的文件

这将search文件的整个目录

'UDF for finding a file no matter where it is located, providing it is within the HostFolders minimum location criteria 'The first function checks if the scripting runtime reference is enabled on hosts computer, and if not, enables it so that the FileSytemObject may be created 'The second function will find a specified file by begining with the intial stated HostFolder, and then go through all subfolders until it has found the file Option Explicit Dim FileSystem As Object Dim HostFolder As String Dim Ref As Object, CheckRefEnabled% Sub FindFile() HostFolder = "Insert folder path here\" 'Change this HostFolder to suit preference CheckRefEnabled = 0 With ThisWorkbook For Each Ref In .VBProject.References If Ref.Name = "Scripting" Then CheckRefEnabled = 1 Exit For End If Next Ref If CheckRefEnabled = 0 Then .VBProject.References.AddFromFile ("C:\Windows\System32\scrrun.dll") End If End With Set FileSystem = CreateObject("Scripting.FileSystemObject") DoFolder FileSystem.GetFolder(HostFolder) End Sub Sub DoFolder(Folder) Dim SubFolder For Each SubFolder In Folder.SubFolders DoFolder SubFolder Next Dim File For Each File In Folder.Files If File.Name = "insert name of file here.xlsb" Then Workbooks.Open (Folder.Path & "\" & File.Name), UpdateLinks:=False Workbooks(File.Name).Activate Exit Sub End If Next End Sub 

代码的下一部分实现以下内容:

将ThisWorkbook中的工作表复制到新打开的工作簿,保存并closures它

 Option Explicit Dim w As Workbook Sub sheetCopy() Set w = ThisWorkbook 'amend this to suit your workbook names and sheet names w.Sheets("Sheet1").Copy after:=Workbooks("workbook name").Sheets(1) End Sub 

所以,最后运行下面的完整代码来完成一切

 Option Explicit Dim w As Workbook Sub sheetCopy() Set w = ThisWorkbook FindFile 'amend this to suit your workbook names and sheet names w.Sheets("Sheet1").Copy after:=Workbooks("workbook name").Sheets(1) End Sub