【发布时间】:2014-11-12 00:26:33
【问题描述】:
我有三个文件 A.csv、B.xls 和 C.xls,它们都在同一个目录中
我想从每个文件中取出最左边的 7 列,忽略顶部的标题行,但使用 VBA 将所有其他行放在一张表(组合表)中。
在 Sheet1 中,我已将文件名添加到单元格 C3 到 C5 中的文件,以及 C1 中的目录路径。
活动工作簿的名称是 Book2.xlsm
以下是我迄今为止尝试实现此目的的代码:
Sub combineSheets()
Dim directory As String, fileName As String
Dim fileRng As Range
Dim fileCell As Range
directory = Sheet1.Range("C1").Value
Set fileRng As Sheets("Sheet1").Range("C3")
Set fileRng As Range(fileRng,fileRng.End(xlDown))
For Each fileCell in fileRng
fileName = fileCell.Value
'Application.DisplayAlerts = False
Workbooks.Open (directory & fileName)
Workbooks(fileName).Select
Sheet1.Select
Range("A2").Select
Range(Selection, Selection.Offset(Selection.End(xlDown).Row - 2, 7)).Select
'need code to work out how to add on the bottom
Selection.Copy Destination:=Workbooks("Book2.xlsm").Sheets("combined").Range("A2")
'Application.DisplayAlerts = True
Next
Workbooks("Book2.xlsm").Sheet1.Select
End Sub
【问题讨论】: