遍历 excel 个文件以在主文件中复制、转置和粘贴数据集
looping through excel files to copy, transpose and paste set of data in a master file
此处的代码范围从文件夹中的每个 excel 文件复制并粘贴到主文件中。我想要它复制、转置和粘贴它,但这段代码无法做到这一点。
请帮助并提前致谢。
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
dum = "D:\MACROS Test folder\"
MyFile = Dir(dum)
Do While Len(MyFile) > 0
If MyFile = "Z_Macro .xlsm" Then
Exit Sub
End If
Workbooks.Open (dum + MyFile)
Range("F17:F24").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1),
Cells(erow, 4))
MyFile = Dir
Loop
End Sub
您可以查看之前的一些问题,其中包含有关使用 VBA 以转置方式粘贴数据的解决方案。
Excel VBA - Range.Copy transpose paste
Copy and Paste (transposed & values) a set range in a different sheet in the next empty row
VBA Copy & Transpose Data Range
Transpose a range in VBA
Trouble with PasteSpecial Transpose
VBA Code - Copy and Transpose paste with specific conditions
检查完这些之后,如果您仍然遇到问题,请返回并向我们展示您尝试过的代码并描述问题出在哪里,也许有人可以帮助您。
感谢@ed2 的建议。在完成这些答案之后,我以某种方式设法让我的代码做我想做的事,只需稍作修改。所以这是代码
Option Explicit
Sub allmacros()
Call LoopThroughDirectory
Call transpose_copy
End Sub
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow As Variant
Dim dum As Variant
Dim bswitch As Boolean
dum = "D:\excel vba\New folder\"
MyFile = Dir(dum)
Do While Len(MyFile) > 0
If MyFile = "Master.xlsx" Then
Exit Sub
End If
Workbooks.Open (dum + MyFile)
Range("B1:B4").Copy
ActiveWorkbook.Close
erow = Sheet3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("sheet3").Range(Cells(erow, 1), Cells(erow, 4))
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
MyFile = Dir
Loop
End Sub
Sub transpose_copy()
Dim x As Integer
x = 2
Do Until IsEmpty(Cells(x, 1))
Range("A" & x & ":A" & x + 3).Select
Selection.Copy
Worksheets("report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True
x = x + 4
Loop
End Sub
这样做的目的是遍历文件夹中的 excel 个文件,然后复制这些数据并将其粘贴到主文件中的空 sheet 中。然后它将数据从空sheet复制并转置到主文件
中的报告sheet
此处的代码范围从文件夹中的每个 excel 文件复制并粘贴到主文件中。我想要它复制、转置和粘贴它,但这段代码无法做到这一点。 请帮助并提前致谢。
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
dum = "D:\MACROS Test folder\"
MyFile = Dir(dum)
Do While Len(MyFile) > 0
If MyFile = "Z_Macro .xlsm" Then
Exit Sub
End If
Workbooks.Open (dum + MyFile)
Range("F17:F24").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1),
Cells(erow, 4))
MyFile = Dir
Loop
End Sub
您可以查看之前的一些问题,其中包含有关使用 VBA 以转置方式粘贴数据的解决方案。
Excel VBA - Range.Copy transpose paste
Copy and Paste (transposed & values) a set range in a different sheet in the next empty row
VBA Copy & Transpose Data Range
Transpose a range in VBA
Trouble with PasteSpecial Transpose
VBA Code - Copy and Transpose paste with specific conditions
检查完这些之后,如果您仍然遇到问题,请返回并向我们展示您尝试过的代码并描述问题出在哪里,也许有人可以帮助您。
感谢@ed2 的建议。在完成这些答案之后,我以某种方式设法让我的代码做我想做的事,只需稍作修改。所以这是代码
Option Explicit
Sub allmacros()
Call LoopThroughDirectory
Call transpose_copy
End Sub
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow As Variant
Dim dum As Variant
Dim bswitch As Boolean
dum = "D:\excel vba\New folder\"
MyFile = Dir(dum)
Do While Len(MyFile) > 0
If MyFile = "Master.xlsx" Then
Exit Sub
End If
Workbooks.Open (dum + MyFile)
Range("B1:B4").Copy
ActiveWorkbook.Close
erow = Sheet3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("sheet3").Range(Cells(erow, 1), Cells(erow, 4))
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
MyFile = Dir
Loop
End Sub
Sub transpose_copy()
Dim x As Integer
x = 2
Do Until IsEmpty(Cells(x, 1))
Range("A" & x & ":A" & x + 3).Select
Selection.Copy
Worksheets("report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True
x = x + 4
Loop
End Sub
这样做的目的是遍历文件夹中的 excel 个文件,然后复制这些数据并将其粘贴到主文件中的空 sheet 中。然后它将数据从空sheet复制并转置到主文件
中的报告sheet