打开由竖线字符““分隔的csv文件|“或不是常用分隔符

gdrx4gfi  于 2023-02-06  发布在  其他
关注(0)|答案(9)|浏览(259)

我正在尝试设置一个Excel VBA代码,该代码可以打开一些.csv文件,并将包含的信息拆分为列,并由字符|分隔。我设法打开了该文件,但我使用的代码打开文件时没有根据分隔符拆分文本。到目前为止,我尝试了以下代码:

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
lh80um4z

lh80um4z1#

我记得以前这件事让我发疯。
Excel似乎对.csv文件有一种不受控制的贪婪,如果你只是改变结尾(.txt.dat或其他什么),它就会工作!

8nuwlpux

8nuwlpux2#

我试过这样做,但不起作用。但是如果你尝试在文本文件上做同样的事情(通过复制粘贴csv内容到文本文件),它会起作用。
如果你看一下MSDN Link,它在“workbooks.open”方法的“Delimiter”参数的描述中特别提到**“如果它是一个文本文件”**,也许这就是它不工作的原因。
我不确定。这对我来说也是一件新鲜事。希望这对我有帮助。

pkwftd7m

pkwftd7m3#

Rowan的解决方案确实有效。关键是将解决方案中的文件名“Test.csv”替换为“\CSV_Files\”位置中的“Test.txt”。“Test.txt”不应是逗号分隔值类型。它应该是真正的TXT文件类型。
在Windows资源管理器中检查文件类型。确保它不是CSV。如果使用CSV类型,实际上将告诉Excel数据是由逗号而不是管道分隔符解析的。
如果工作簿位于根目录下:c:\创建目录:C:\CSV_Files放入文本文件:目录\CSV_Files中的Test.txt
在您的工作簿中打开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并运行宏。

u4vypkhs

u4vypkhs4#

试试看

Delimiter:= Chr(124)

字符124是管道"|"

jbose2ul

jbose2ul5#

我想这对你有帮助。

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
w6mmgewl

w6mmgewl6#

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
ercv8c1e

ercv8c1e7#

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
goqiplq2

goqiplq28#

我一直有问题的工作簿。打开的分隔符,在一些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
mwngjboj

mwngjboj9#

以前的答案对这篇文章有帮助,我想添加这个参考更改文件扩展名与VBA的情况下,你有可能是有用的合并:
how-to-change-file-extensions-in-VBA

相关问题