我已经创建了一个VBA代码,它可以帮助我整合来自不同工作簿的数据,并粘贴到我的目标工作表中,同时粘贴数据,它正在粘贴公式,因为#REF!是来在所有的细胞,我想粘贴我的数据在PasteAsspecial。
Sub ConsolidateData()
Dim sourcePath As String
Dim folderName As Variant
Dim sourceFile As String
Dim SourceBook As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastrow As Long
Dim targetRow As Long
Dim i As Long
'Set the path to the source Directory
sourcePath = "C:\Windows\Blackstone\Software\Box\Internal Control\L1 & L2 Review Project\Checklist"
'Set the target sheet
Set targetSheet = ThisWorkbook.Worksheets("Overview")
'Copy Column headers to target sheet if it's empty
If Application.CountA(targetSheet.Range("A1:XFD1")) = 0 Then
targetSheet.Range("A1:XFD1").Value = Array("Tab Name", "Activity", "SOP Status", "CQ Quarter Transition", "Quarter of Transition", "Source Systems", "Exclusions/Exceptions", "Comments", "Criticality", "Time spent L1 (mins)", "Est. Time Spent L2 (mins)", "L2 Applicable")
End If
'Loop through each folder in the source directory
For Each folderName In Array("Asia Business Finance", "Asia Finance I PM", "BEPIF FPA", "BPP Finance", "BREDS AM", "BREDS Loan Ops", "BREIT FP&A and PM", "BREP Finance", "Common Activities", "EMEA PM", "Europe PM", "US PM")
'set the path to the source folder
sourcePath = "C:\Windows\Blackstone\Software\Box\Internal Control\L1 & L2 Review Project\Checklist\Chicklist2\" & folderName & "\"
'Loop through each file in the source folder
sourceFile = Dir(sourcePath & "*.xlsx")
Do While sourceFile <> ""
'Open the sourceWorkbook
Set SourceBook = Workbooks.Open(sourcePath & sourceFile)
'set the source sheet
Set sourceSheet = SourceBook.Worksheets("Overview")
'Find the last row in the source sheet
lastrow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
'loop through each row in the source sheet
For i = 2 To lastrow
'Check if the row meets the criteria for copying
If sourceSheet.Cells(i, "A").Value <> "" Then
'copy the row to the target sheet
targetRow = targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Row + 1
sourceSheet.Range("A" & i & ":L" & i).Copy targetSheet.Range("B" & targetRow & ":M" & targetRow)
targetSheet.Cells(targetRow, "A").Value = folderName 'Add folder name to column A
End If
Next i
'Close the sourceWorkbook
SourceBook.Close
'Get the next file in the directory
sourceFile = Dir
Loop
Next folderName
'Save the target workbook
ThisWorkbook.Save
End Sub
1条答案
按热度按时间guicsvcw1#
快速和简单的方法来获得粘贴特殊:
但你也可以用途:
更快的方法是使用数组,这样你就不会逐行添加:
(don别忘了我确实编辑/添加了一些变量)希望对你有效:)