我正在将数据从CSV导入到MS Word中。当我尝试使用内置的split()函数拆分数据时,就会出现我的问题。它应该很简单,但是当我尝试拆分它时,出现“类型不匹配”错误。这是我的第一行文字,显示在立即窗口中:
"Acct","SSN","Name","Last name","Phone1","Phone2","Address","City","State","Ph home","Ph work","Ph mobile","Ph other","Email","Notes","Zip"
但是,当我在调试过程中将鼠标悬停在变量上时,我认为整行都被视为字符串:
""Acct","SSN","Name","Last name","Phone1","Phone2","Address","City","State","Ph home","Ph work","Ph mobile","Ph other","Email","Notes","Zip""
我假设在导入时,将行的每个元素都视为一个字符串,然后将行本身变成一个巨型字符串?我真的不确定。现在,我要做的只是将行分成两部分,并对照预定变量检查第一项。我还没有使用此功能,而且我对Word上的VBA还不太了解。
简而言之,如何将这一行视为可以与之交互以将其拆分为元素的数据类型?我认为这都被视为一个要素,因此给我带来了问题。
Private Function importCSV_(x)
Dim filenum As Integer
Dim arrLines() As String
Dim arrSplit As Variant
Dim textline As String
Dim lines
filenum = FreeFile()
Open "\\...\studentData.csv" For Input As #filenum
Do Until EOF(filenum)
Line Input #filenum, textline
lines = lines + 1
Debug.Print textline
Debug.Print split(textline, ",", 2) '<--- ERROR APPEARS HERE
arrSplit = split(textline, ",", 2)
ReDim Preserve arrLines(lines)
arrLines(lines) = arrSplit
If checkForFour_(arrSplit(0)) = True Then
arrSplit(UBound(arrLines)) = split(arrLines(lines), ",")
Else
arrSplit(UBound(arrLines) - 1) = split(arrLines(lines) - 1) & "," & split(arrLines(lines), ",")
End If
Loop
Debug.Print UBound(arrLines)
Debug.Print UBound(arrSplit)
End Function
checkForFour_函数是一个私有函数,仅检查数据输入的格式。我仍在学习编程,而且我自己也正在学习编程,因此我一直在简化工作,从而创建了一些垃圾代码。
答案 0 :(得分:0)
轻微语法错误:
Debug.Print split(textline, ",")(2)
那应该为您做。元素在数组后面放在方括号中。