打开由管道符“|”分隔的csv文件或不常见的分隔符

时间:2013-07-24 11:25:21

标签: vba excel-vba excel

我正在尝试设置一个excel vba代码,用于打开一些csv文件,并将包含的信息拆分为列,并用字符“|”分隔。我设法打开文件,但我使用的代码打开我的文件,而不根据分隔符拆分文本。我从2天开始就坚持这个。 到目前为止,我已经尝试了以下代码:

Sub OpenCSV()

Dim wkbTemp As Workbook
Dim sPath As String, sName As String

sPath = ThisWorkbook.Path & "\CSV_Files\"
sName = "Test.csv"

Set wkbTemp = Workbooks.Open(Filename:=sPath & sName, Format:=6, Delimiter:="|")

End Sub

有人有建议吗?

由于

阿尔贝托

8 个答案:

答案 0 :(得分:2)

我试过这样做。它不起作用。但是如果你尝试在文本文件上做同样的事情(通过将csv内容复制粘贴到文本文件中),它就可以了。

如果您查看MSDN Link,则在'workbooks.open'方法的'Delimiter'参数说明中明确指出',如果它是文本文件。也许这就是它不起作用的原因。

我不确定。这对我来说也是一件新事。希望这会有所帮助。

答案 1 :(得分:2)

我记得这让我精神错乱了一段时间 似乎Excel对.csv文件有一种不受控制的贪婪。如果您只是更改结尾(.txt.dat或其他),它将会起作用!

答案 2 :(得分:0)

尝试

Delimiter:= Chr(124)

字符124是管道“|”

答案 3 :(得分:0)

我认为这应该对你有帮助。

Sub OpenCSV()

Dim wkbTemp As Workbook
Dim sPath As String, sName As String

sPath = ThisWorkbook.Path & "\CSV_Files\"
sName = "Test.csv"
 Workbooks.OpenText Filename:=sPath & sName, _
    Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
    , Comma:=False, Space:=False, Other:=True, OtherChar:="|"
Set wkbTemp = ActiveWorkbook

end sub

答案 4 :(得分:0)

Rowan的解决方案确实有效。关键是在“\ CSV_Files \”位置用“Test.txt”替换其解决方案中的文件名“Test.csv”。 “Test.txt”不应是逗号分隔值类型。它应该是真正的TXT文件类型。

在Windows资源管理器中检查文件类型。确保它不是CSV。如果您使用CSV类型,您实际上会告诉Excel数据是用逗号而不是管道分隔符解析的。

如果您的工作簿是root用户:c:\ 创建目录:C:\ CSV_Files 将文本文件:Test.txt放在目录\ CSV_Files

在您的工作簿中打开VBA并复制下面的完整VBA代码。

完整的VBA代码应为:

Sub OpenCSV()

Dim wkbTemp As Workbook
Dim sPath As String, sName As String

sPath = ThisWorkbook.Path & "\CSV_Files\"
sName = "Test.txt"
 Workbooks.OpenText Filename:=sPath & sName, _
    Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
    , Comma:=False, Space:=False, Other:=True, OtherChar:="|"
Set wkbTemp = ActiveWorkbook

end sub

关闭VBA并运行宏。

答案 5 :(得分:0)

Option Explicit
Private Sub Text2Excel()
Dim excel_app As Excel.Application
Dim max_col As Integer
Dim txtFromFile As Variant
Dim Sep As String

    'DoEvents

Application.ScreenUpdating = False

    txtFromFile = Application.GetOpenFilename(FileFilter:="Text Files (*.txt),*.txt," & _
                                                "CSV Files (*.csv),*.csv")
    If txtFromFile = False Then
        ''''''''''''''''''''''''''
        ' user cancelled, get out
        ''''''''''''''''''''''''''
        Exit Sub
    End If

    Sep = Application.InputBox("Enter a separator character." & vbNewLine & "For TAB Delimited keep BLANK.", Type:=2)

    If Sep = vbNullString Then
        ' user cancelled, get out
       Sep = vbTab
    End If

    'Pull the data from test file to activesheet

    Workbooks.OpenText FileName:=txtFromFile, DataType:=xlDelimited, Other:=True, otherchar:=Sep, local:=True

    MsgBox "Data from selected file " & txtFromFile & " has been pulled to Excel.", vbInformation

Application.ScreenUpdating = False

End Sub

答案 6 :(得分:0)

Sub CSVtoXLS()
Dim xFd As FileDialog
Dim xSPath As String
Dim xCSVFile As String
Dim xWsheet As String
Dim xOtherChar As String
    Application.DisplayAlerts = False
    Application.StatusBar = True
    xWsheet = ActiveWorkbook.Name
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    xFd.Title = "Select a folder:"
    If xFd.Show = -1 Then
            xSPath = xFd.SelectedItems(1)
    Else
        Exit Sub
    End If
    If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\"
    xCSVFile = Dir(xSPath & "*.csv")
    xOtherChar = InputBox("Please indicate delimiter:", "CSV file/Text to column Converter", ",")
    Do While xCSVFile <> ""
        Application.StatusBar = "Converting: " & xCSVFile
        Workbooks.OpenText Filename:=xSPath & xCSVFile, DataType:=xlDelimited, Tab:=True, Other:=True, OtherChar:=";"
        ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare), xlWorkbookDefault
        ActiveWorkbook.Close
        Windows(xWsheet).Activate
        xCSVFile = Dir
    Loop
    Application.StatusBar = False
    Application.DisplayAlerts = True
End Sub

答案 7 :(得分:0)

Workbooks.Open 的分隔符一直有问题,在某些 CSV 上它无法正常工作。

所以对于这些工作表,我使用以下代码:

用您的分隔符替换 sDelimiter

Dim sDelimiter as string: sDelimiter = "|"

Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, Other:=True, OtherChar:=sDelimiter

禁用 DisplayAlerts 如果它一直提示(在使用 TextToColumns 之前禁用 DisplayAlerts)

Application.DisplayAlerts = False