我试图将其他工作簿中的数据提取到主工作簿中。所有这些工作簿都保存在一个文件夹中。我想自动打开其他工作簿而不是手动打开。我需要提取的数据是非相邻单元格,我希望从每个源工作簿中提取的数据在主工作簿中以行显示(因为我在第1行中有一个头行,所以从第一个工作簿中提取数据后并粘贴在第2行,从第二个工作簿中提取的数据将列在第3行,依此类推)
然而,当我运行宏时,我陷入了Transpose。
以下是我的代码
Sub LoopThroughDirectory()
Dim MyFile As String
Dim wkbSource As Workbook
Dim wkbTarget As Workbook
Dim erow As Single
Dim Filepath As String
Dim copyRange As Range
Dim cel As Range
Dim pasteRange As Range
Filepath = "C:\xxxxx\"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "Import Info.xlsm" Then GoTo NextFile
Set wkbTarget = Workbooks("Import Info.xlsm")
Set wkbSource = Workbooks.Open(Filepath & MyFile)
Set copyRange = wkbSource.Sheets("sheet1").Range("c3,f6,f9,f12,f15,f19,f21,f27,f30,f33,f37,f41")
Set pasteRange = wkbTarget.Sheets("sheet1").Range("a1")
For Each cel In copyRange
cel.Copy
ecolumn = wkbTarget.Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Column
wkbTarget.Worksheets("Sheet1").Paste Destination:=wkbTarget.Worksheets("Sheet1").Range(Cells(1, ecolumn).Address)
pasteRange.Cells(1, ecolumn).PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
wkbSource.Close
NextFile:
MyFile = Dir
Loop
End Sub
到目前为止我做了什么:
请帮帮我。非常感谢你!
答案 0 :(得分:0)
您必须在循环之外设置目标范围并将其偏移到循环内部:
cUnmanagedInterpolator