我正在尝试逐行将文本文件加载到数据库中,我不能使用标准的导入方法,因为文件的细分意味着每行有不同的列数,但幸运的是以逗号分隔。
我正在尝试使用“拆分”功能,以便我可以从每一行中提取某些信息,不幸的是,任何文本字段都被“”封闭,而某些文本字段将包含指定文本中的逗号。
是否可以使用Split函数并忽略封闭文本中的任何逗号,如果没有,那么我很乐意编写自定义函数但是可以确定文本字符串是否被“”包围? / p>
谢谢&亲切的问候,
太
答案 0 :(得分:0)
我不认为可以使用Split
函数执行此操作。我最近试图做同样的事情(但用括号而不是引号)。我有我编写的自定义函数,我将其概括为接受任何分隔符和任意数量的“引用字符”对;例如,(),[],{},“”等
Function SplitUnquotedSections(Text As String, Delimiter As String, _
ParamArray QuotePairs()) As Collection
Dim Char As String, Word As String, i As Integer, Pos As Long, Unquote As String
Set SplitUnquotedSections = New Collection
For Pos = 1 To Len(Text)
Char = Mid(Text, Pos, 1)
If Len(Unquote) = 0 Then
If Char = Delimiter Then
SplitUnquotedSections.Add Word
Word = ""
Else
For i = LBound(QuotePairs) To UBound(QuotePairs) Step 2
If Char = QuotePairs(i) Then
Unquote = QuotePairs(i + 1)
Exit For
End If
Next i
Word = Word & Char
End If
Else
Word = Word & Char
If Char = Unquote Then
Unquote = ""
End If
End If
Next Pos
If Len(Word) > 0 Then SplitUnquotedSections.Add Word
End Function
该函数返回一个集合,因此您可以像这样使用它:
Sub TestSplit()
Const TestString As String = """Times Square"", ""New York, NY"", 10011"
Dim Item As Variant
For Each Item In SplitUnquotedSections(TestString, ",", """", """")
Debug.Print Trim(Item)
Next Item
End Sub
"Times Square"
"New York, NY"
10011
答案 1 :(得分:0)
非常感谢mwolfem我很遗憾地说我还没有测试过您的代码,但在我的情况下,我希望从我的字符串中选择某些值而不打印整行,但是经过一些研究,我有一个正在努力解决这个问题的任何其他人的工作解决方案如下:
在模块中创建第一个函数:
Public Function DelimConvertor(varInput As Variant, strDelim As String, strQualifier As String) As Variant
Const strStart = "Ã" 'Ascii 195, or any other Ascii you are unlikely to encounter
Const strEnd = "ž" 'Ascii 158, or any other Ascii you are unlikely to encounter
Const strNewDelim = "§" 'Ascii 167, or any other Ascii you are unlikely to encounter
Dim lngi As Long
Dim strChar As String
Dim flgStart As Boolean
Dim varOutput As Variant
'Format the string so we can distinguish the fields
If Left(varInput, 1) = strQualifier Then
varInput = strStart & Right(varInput, Len(varInput) - 1)
End If
If Right(varInput, 1) = strQualifier Then
varInput = Left(varInput, Len(varInput) - 1) & strEnd
End If
varInput = Replace(varInput, strDelim & strQualifier, strDelim & strStart)
varInput = Replace(varInput, strQualifier & strDelim, strEnd & strDelim)
'Loop through and format the rest of the string
For lngi = 1 To Len(varInput)
strChar = Mid(varInput, lngi, 1)
Select Case strChar
Case strStart
flgStart = True
Case strEnd
flgStart = False
Case Else
If flgStart Then
varOutput = varOutput & strChar
ElseIf strChar = strDelim Then
varOutput = varOutput & strNewDelim
Else
varOutput = varOutput & strChar
End If
End Select
Next
'Return the cleansed stream that can now be used with the Split function
DelimConvertor = varOutput
End Function
然后,您在文件导入期间使用它,如下所示:
Public Function Test()
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim strArray() As String
Set ts = fso.OpenTextFile("C:\Users\Admin\Desktop\test.txt", ForReading)
strArray = Split(DelimConvertor(ts.ReadLine, ",", """"), "§")
'Obtain your desired field here e.g. strArray(0)
ts.Close
End Function