【发布时间】:2020-08-21 23:39:49
【问题描述】:
我正在尝试将文件夹中多个工作簿中的数据复制到一个电子表格中。此代码有效,只是我似乎无法调整它以仅粘贴值。有人可以告诉我如何编辑“'将“SearchCaseResults”表上的数据复制到其他工作簿中的“Disputes”表“下的行,以便粘贴值而不是公式、边框等。提前致谢!
Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim lRow As Long
Dim ws2 As Worksheet
Dim y As Workbook
'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 = "C:\Users\Ashton\Desktop\Control\"
.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)
Set y = Workbooks.Open("C:\Users\Ashton\Desktop\Control")
Set ws2 = y.Sheets("Sheet1")
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Copy data on "SearchCaseResults" sheet to "Disputes" Sheet in other workbook
With wb.Sheets("Timesheet")
.Range("A9:B18").Copy ws2.Range("A" & Rows.Count).End(xlUp)
.Range("B4").Copy ws2.Range("C" & Rows.Count).End(xlUp)
.Range("S9:S18").Copy ws2.Range("D" & Rows.Count).End(xlUp)
End With
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
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
【问题讨论】:
-
那么您在实施this approach 时遇到了麻烦吗?还是您尝试使用value transfer?