英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论