7

I'm trying to set up an Excel VBA code that opens up some .csv files and split into columns the information contained and delimited by the character |. I manage to open the file but the code I use opens my files without splitting the text according to the delimiter. So far I have tried the following code:

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
4

8 回答 8

4

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

于 2013-07-24T12:12:21.060 回答
2

我试着这样做。它不起作用。但是,如果您尝试对文本文件执行相同操作(通过将 csv 内容复制粘贴到文本文件中),它会起作用。

如果您查看MSDN Link ,它会在 'workbooks.open' 方法的 'Delimiter' 参数的描述中明确表示“如果它是文本文件” 。也许这就是它不起作用的原因。

我不知道。这对我来说也是新事物。希望这可以帮助。

于 2013-07-24T12:11:58.447 回答
1

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

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

如果您的工作簿位于根目录中: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 并运行宏。

于 2013-10-19T22:47:31.853 回答
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
于 2013-07-24T12:13:51.740 回答
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
于 2014-11-07T13:32:53.863 回答
0

试试看

Delimiter:= Chr(124)

字符 124 是竖线“|”

于 2013-07-24T12:04:16.840 回答
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
于 2017-12-15T05:21:52.973 回答
0

我一直遇到 Workbooks.Open 的分隔符问题,在某些 CSV 上它不能正常工作。

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

替换sDelimiter为您的分隔符。

Dim sDelimiter as string: sDelimiter = "|"

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

DisplayAlerts如果一直提示则禁用(使用前禁用DisplayAlerts TextToColumns

Application.DisplayAlerts = False
于 2021-05-09T17:47:30.773 回答