【发布时间】:2017-01-25 20:22:03
【问题描述】:
我想:
1- 检索文件夹中的所有 csv 文件。
2- 将文件名放在新工作表的第一行。
3- 从 csv 文件中复制所有列标题(第一行)。
4- 将它们粘贴到新工作表中每个文件名下的一列中。
我知道我需要使用范围,但我不知道如何使用它。
这就是我为获取 csv 文件所做的工作。我在将行复制到列时遇到问题:
Dim CSVPath
Dim FS
Dim file
Dim wkb As Excel.Workbook
Dim ResultsSheet As Worksheet
Dim RowPtr As Range
Dim CSVUsed As Range
Set ResultsSheet = Sheet1
'Clear the results sheet
ResultsSheet.Cells.Delete
Set FS = CreateObject("Scripting.FileSystemObject")
'The CSV files are stored in a "CSV" subfolder of the folder where
'this workbook is stored.
CSVPath = ThisWorkbook.Path & "\CSV"
If Not FS.FolderExists(CSVPath) Then
MsgBox "CSV folder does not exist."
Exit Sub
End If
For Each file In FS.GetFolder(CSVPath).Files
If Right(file.Name, 3) = "csv" Then 'Only look at files with .csv extension
Set wkb = Application.Workbooks.Open(file.Path)
Next
我也知道循环应该是这样的:
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To FinalRow
【问题讨论】: