我有一个代码可以通过.xlsx,.xls和.csv文件(很多文件)。但.csv文件数据通常都在A列中,由" |"分隔。如何在循环浏览这些文件并提取提取之前先分隔这些文件?它可能很复杂,因为有时不仅Col A有数据,而Col B可能有几行。
EDIT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub ColumnHeaders()
'includes filling down
'Skips unreadable files
Dim wb As Workbook, fileNames As Object, errCheck As Boolean
Dim wsReport As Excel.Worksheet
Set wsReport = ActiveWorkbook.Sheets("Sheet1") 'Whatever sheet you want to write to
Dim lRow As Long
lRow = 1
' Turn off screen updating and automatic calculation
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
'get user input for files to search
Set fileNames = CreateObject("Scripting.Dictionary")
errCheck = UserInput.FileDialogDictionary(fileNames)
If errCheck Then
Exit Sub
End If
'''
For Each Key In fileNames 'loop through the dictionary
On Error Resume Next
Set wb = Workbooks.Open(fileNames(Key))
If Err.Number <> 0 Then
Set wb = Nothing ' or set a boolean error flag
End If
On Error GoTo 0 ' or your custom error handler
If wb Is Nothing Then
wksSkipped.Cells(wksSkipped.Cells(wksSkipped.Rows.Count, "A").End(xlUp).Row + 1, 1) = fileNames(Key)
Else
Debug.Print "Successfully loaded " & fileNames(Key)
wb.Application.Visible = False 'make it not visible
'--------------------DATA Extraction ----------------------------------------
Dim iIndex As Integer
Dim lCol As Long
Dim lOutputCol As Long
'Loop through the worksheets in the current workbook.
For iIndex = 1 To wb.Worksheets.Count
'Set the current worksheet
Set ws = Application.Worksheets(iIndex)
'List out the workbook and worksheet names
wsReport.Range("A" & lRow).Value = wb.Name
wsReport.Range("B" & lRow).Value = ws.Name
'Start a counter of the columns that we are writing to
lOutputCol = 3
'Loop through the columns.
For lCol = 1 To ws.UsedRange.Columns.Count
'Write the header
wsReport.Range(Col_Letter(lOutputCol) & lRow).Value = ws.Range(Col_Letter(lCol) & "1").Value
'Increment our column counters.
lOutputCol = lOutputCol + 1
Next lCol
'Increment the row we are writing to
lRow = lRow + 1
Next iIndex
'-----------------------Data Extraction END-------------------------------------
wb.Close savechanges:=False 'close the workbook do not save
Set wb = Nothing 'release the object
End If
Next 'End of the fileNames loop
Set fileNames = Nothing
' Reset system settings
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.Visible = True
End With
End Sub
答案 0 :(得分:0)
两种可能的方法。
APPROACH#1
您的代码首先打开文件作为文本,然后将(作为第一行插入)添加到文件中:
sep=|
接下来,您的代码将在Excel中以CSV格式打开。如果您的文本限定符是正常引号,那么这将非常有效。
我知道在VBA中预先添加有点棘手。如果文件不是太大,那么您将创建一个新文件,其中第一行后跟原始文件的其余部分(例如http://www.ozgrid.com/forum/showthread.php?t=163510)。
这种方法的优点是您的VBA不需要进行文本到列等。
方法#2 在执行文本到列之前,VBA会循环遍历所有行和列以进行清理。正如你所说,如果分隔符是非标准的,你通常会在B或C列中得到最终值,甚至更多,所以你需要&#34;清理&#34;首先将它们全部放回A列。
我曾经运行自己的小宏来完成这个清理,正是因为这个原因,即&#34; |&#34;作为分隔符。
后来,我发现我可以插入&#34; sep = |&#34; (例如,使用Notepad ++),然后在Excel中打开。
我写了一篇关于这两种方法的简短文章,并在这里提供了我的代码:https://wildwoollytech.wordpress.com/2015/12/11/combine-excel-cells-into-one/