VBA -在多维数组中拆分CSV文件

mm5n2pyu  于 2022-12-25  发布在  其他
关注(0)|答案(2)|浏览(252)

我有一个.csv,这是在以下格式:

[TestHeader]FIELDS,"LANGUAGE","LC_NUMERIC","CELLNAME","TESTTYPE","REPORTER","TITLE","STARTDATE","STARTTIME","ENDDATE","ENDTIME"DATATYPE,"Text(80)","Text(80)","Text(64)","Text(80)","Text(80)","Text(80)","Text(12)","Text(20)","Text(12)","Text(20)"

我想把这个数据放在一个多维数组中,这个数组可以模拟它在一个工作表中。如果单元格是空的,那么它在数组中也是空的。

我尝试使用以下代码,但它只将数据放入一个一维数组中,这不适合我的需要。

Dim Delimiter As String
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim LineArray() As String
Dim DataArray() As String

'Inputs
  Delimiter = ","
  FilePath = emiFilePath
  
'Open the text file in a Read State
  TextFile = FreeFile
  Open FilePath For Input As TextFile
  
'Store file content inside a variable
  FileContent = Input(LOF(TextFile), TextFile)

'Close Text File
  Close TextFile
  
'Separate Out lines of data
  LineArray() = Split(FileContent, Delimiter, -1, vbTextCompare)

'Read Data into an Array Variable
      'Re-Adjust Array boundaries
        ReDim Preserve DataArray(UBound(LineArray))
'
      'Load line of data into Array variable
        For y = LBound(LineArray) To UBound(LineArray)
          DataArray(y) = Replace(LineArray(y), Chr(34), vbNullString)
        Next y
ej83mcc0

ej83mcc01#

在@Ralph和@VincentG的帮助下

Dim Delimiter As String
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim LineArray() As String
Dim DataArray() As Variant

'Inputs
  Delimiter = ","
  FilePath = emiFilePath

'Open the text file in a Read State
  TextFile = FreeFile
  Open FilePath For Input As TextFile

'Store file content inside a variable
  FileContent = Input(LOF(TextFile), TextFile)

'Close Text File
  Close TextFile

'Separate Out lines of data
  LineArray() = Split(FileContent, vbLf, -1, vbTextCompare)

'Read Data into an Array Variable
      'Re-Adjust Array boundaries
        ReDim Preserve DataArray(UBound(LineArray))
'
      'Load line of data into Array, separate by commas and remove unwanted blank strings
        For y = LBound(LineArray) To UBound(LineArray)
          DataArray(y) = Split(Replace(LineArray(y), Chr(34), vbNullString), Delimiter)
        Next y
mhd8tkvw

mhd8tkvw2#

当您编写代码时:

'Separate Out lines of data
  LineArray() = Split(FileContent, Delimiter, -1, vbTextCompare)

您不是在分隔行,而是在分隔由分隔符“”分隔的字段
如果你的简历使用Windows风格的行尾,首先在vbCrLf上分割你的数据。

Function mySplit(ByVal emiFilePath As String) As Variant()
    Dim Delimiter As String
    Dim TextFile As Integer
    Dim FilePath As String
    Dim FileContent As String
    Dim LineArray() As String
    Dim DataArray() As Variant
    'Inputs
    Delimiter = ","
    FilePath = emiFilePath

    'Open the text file in a Read State
    TextFile = FreeFile
    Open FilePath For Input As TextFile

    'Store file content inside a variable
    FileContent = Input(LOF(TextFile), TextFile)

    'Close Text File
    Close TextFile

    'Separate Out lines of data
    LineArray = Split(FileContent, vbCrLf, -1, vbTextCompare)
    ReDim DataArray(LBound(LineArray) To UBound(LineArray))
    Dim i As Long
    'Separate fields inside the lines
    For i = LBound(LineArray) To UBound(LineArray)
      DataArray(i) = Split(LineArray(i), Delimiter, -1, vbTextCompare)
    Next i
    mySplit = DataArray
End Function

相关问题