0
我在一个目录中有大约500个Excel文件。所有文件在第一张纸上都有一张表(相同尺寸)。我的客户希望他们都在一个单词文件中。我不是一个VBA专家,尝试使用我在网上找到的代码。到目前为止,我已经遍历目录中的所有文件。它还会选择并复制特定范围。但是,如何将文件传输到word文件?循环遍历目录中的所有xls文件,复制范围并粘贴到docx文件?
这里是我做过什么:
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each xls files dir
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Select range and copy
Range("G10:M25").Select
Selection.Copy
'Save and Close Workbook
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
DoEvents
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
我倒没熟悉,但发现这个链接希望它是有用的http://software-solutions-online.com/vba-write-excel-values-to-word-document/ –
嗯。 ..看起来已经很不错了。但是目前为止我还无法实现。 – Rachel