将逗号分隔的字符串转换为数组以进行文本转换

huangapple go评论58阅读模式
英文:

Convert Comma Separated String to Array for Text Conversion

问题

我创建了一个应用程序[在Access中],用于将文本文件转换为Excel文件,因为我的公司经常需要这样做。因此,我创建了一个表格,其中包括文件名、列数和列数据类型的逗号分隔列表。

一切都正常,除了我无法使逗号分隔的列表作为数组工作。首先,我调用ImportTextFile:

Call ImportTextFile("TestFileName", 7, ConvertStringToArray(",,,,,,2"))

然后我调用ConvertStringToArray:

Function ConvertStringToArray(ByVal StringToConvert As String) As Variant

      Dim rawArray() As String
      Dim varArray() As Variant

      rawArray = Split(StringToConvert, ",")
      ReDim varArray(LBound(rawArray) To UBound(rawArray))

      Dim i As Long: For i = LBound(rawArray) To UBound(rawArray)
           varArray(i) = rawArray(i)
      Next i
      ConvertStringToArray = varArray

End Function

然后它传递给ImportTextFile(到目前为止,aDataTypes被传递为数组):

Public Sub ImportTextFile(ByVal strFileName As String, ByVal iNumOfCols As Integer, Optional aDataTypes As Variant = Nothing)

    On Error GoTo Sub_Err
    Dim xl As New Excel.Application: Set xl = New Excel.Application
    xl.DisplayAlerts = False
    Dim sPathAndFile As String: sPathAndFile = cPath & strFileName
    Dim wb As Workbook: Set wb = xl.Workbooks.Add
    Dim ws As Worksheet: Set ws = wb.Sheets(1)
    With ws.QueryTables.Add(Connection:="TEXT;" & sPathAndFile & ".txt", Destination:=ws.Range("$A$1"))
        .FieldNames = True
        .RowNumbers = False
        .RefreshStyle = xlInsertDeleteCells
        .SaveData = True
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        If IsArray(aDataTypes) Then
            .TextFileColumnDataTypes = aDataTypes
        End If
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

End Sub

然而,它在这一行崩溃:

.TextFileColumnDataTypes = aDataTypes

我漏掉了什么?为什么它不起作用?

我收到的错误消息是:

无效的过程调用或参数

英文:

I am creating an application [In Access] to convert text files to excel files because my company does a lot of them. So I created a table that I keep the File Name, Num of Cols, and a 3rd field with the common separated list of the datatypes for the columns.

Everything is working except I cannot get the comma separated list to work as an array. First, I call the ImportText File:

Call ImportTextFile("TestFileName", 7, ConvertStringToArray(",,,,,,2"))

Then I ConvertSTringToArray:

Function ConvertStringToArray(ByVal StringToConvert As String) As Variant

      Dim rawArray() As String
      Dim varArray() As Variant

      rawArray = Split(StringToConvert, ",")
      ReDim varArray(LBound(rawArray) To UBound(rawArray))

      Dim i As Long: For i = LBound(rawArray) To UBound(rawArray)
           varArray(i) = rawArray(i)
      Next i
      ConvertStringToArray = varArray

End Function

Then it passes to ImportTextFile (Up until here aDataTypes is passed as an Array.):

Public Sub ImportTextFile(ByVal strFileName As String, ByVal iNumOfCols As Integer, Optional aDataTypes As Variant = Nothing)

    On Error GoTo Sub_Err
    Dim xl As New Excel.Application: Set xl = New Excel.Application
    xl.DisplayAlerts = False
    Dim sPathAndFile As String: sPathAndFile = cPath & strFileName
    Dim wb As Workbook: Set wb = xl.Workbooks.Add
    Dim ws As Worksheet: Set ws = wb.Sheets(1)
    With ws.QueryTables.Add(Connection:="TEXT;" & sPathAndFile & ".txt", Destination:=ws.Range("$A$1"))
        .FieldNames = True
        .RowNumbers = False
        .RefreshStyle = xlInsertDeleteCells
        .SaveData = True
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        If IsArray(aDataTypes) Then
            .TextFileColumnDataTypes = aDataTypes
        End If
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

End Sub

However, it crashes on this line:

.TextFileColumnDataTypes = aDataTypes

What am I missing? Why isn't this working?

The Error message that I receive is:

> Invalid Procedure Call or Argument

答案1

得分: 1

TextFileColumnDataTypes 需要一个 XlColumnDataType 值的数组,但您传入了一个字符串数组。

也许考虑重新编写您的数组函数:

Function FormatsArray(ByVal StringToConvert As String) As Variant
    Dim i As Long
    Dim rawArray() As String
    Dim varArray As Variant, v

    rawArray = Split(StringToConvert, ",")
    ReDim varArray(LBound(rawArray) To UBound(rawArray))

    For i = LBound(rawArray) To UBound(rawArray)
        v = Trim(rawArray(i))
        If Len(v) > 0 Then     '指定格式?
            varArray(i) = CLng(v)
        Else
            varArray(i) = xlGeneralFormat '使用默认值
        End If
    Next i
    FormatsArray = varArray
End Function
英文:

TextFileColumnDataTypes expects an array of XlColumnDataType values, but you're passing in an array of strings.

Maybe consider reworking your array function:

Function FormatsArray(ByVal StringToConvert As String) As Variant
    Dim i As Long
    Dim rawArray() As String
    Dim varArray As Variant, v

    rawArray = Split(StringToConvert, ",")
    ReDim varArray(LBound(rawArray) To UBound(rawArray))

    For i = LBound(rawArray) To UBound(rawArray)
        v = Trim(rawArray(i))
        If Len(v) > 0 Then     'specific format supplied?
            varArray(i) = CLng(v)
        Else
            varArray(i) = xlGeneralFormat 'use default
        End If
    Next i
    FormatsArray = varArray
End Function

huangapple
  • 本文由 发表于 2023年2月7日 04:56:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定