我正在尝试读取CSV文件并将字符串拆分为工作表列。
所以程序读取CSV,然后读取字符串(获取字符串位置和长度),然后将结果输出到列。
我确实创建了一个执行此操作的宏,但我现在想要使用我以前没有使用过的数组,但似乎无法将数组作为参数传递给SplitColumns函数。
所以字符串规范就像“开始pos,结束pos,格式”,但是现在我没有使用格式。
你可以帮帮我吗?我认为这可能是一个非常简单的问题,我只是在最后一小时坚持这一点。 Option Explicit
Public Sub ConvertPanel()
Dim convertFile As Long, i As Long, y As Long
Dim specString As String
Dim a As Variant
Dim b As Variant
Dim c As Range
Set c = Worksheets(1).Range(Cells(1, 1), Cells(1, 1))
specString = "1,10,@|11,2,@|15,1,@|16,4,@|20,2,@|23,1,@|31,1,@|35,1,@|39,1,@|41,1,@|160,1,@|161,2,@|163,1,@|165,1,@|25,2,@|29,2,@|34,1"
a = QuickRead("file.txt")
b = ConvertSpecString(specString)
End Sub
Private Function ConvertSpecString(ByVal specString As String) As String()
Dim fieldsInfo() As String
Dim inputString As String
inputString = Replace(specString, Space(1), vbNullString)
fieldsInfo = Split(inputString, "|")
ConvertSpecString = fieldsInfo
End Function
Private Function QuickRead(ByVal fileName As String) As String()
Dim fileNumber As Long
Dim stringRes As String
Dim fileSize As Long
Dim v As Variant
fileNumber = FreeFile
fileSize = FileLen(fileName)
stringRes = Space(fileSize)
Open fileName For Binary Access Read As #fileNumber
Get #fileNumber, , stringRes
Close fileNumber
QuickRead = Split(stringRes, vbCrLf)
End Function
Private Function SplitColumns(ByRef lineArray() As Variant, ByRef fieldsInfo() As Variant, ByVal StartCell As Range) As Variant
Dim indexLine As Long
Dim indexCount As Long
Dim stringRange As String
Dim stringColumn As Long
Dim fileInfo As String
stringRange = StartCell
stringColumn = startCell.Column
For indexCount = LBound(lineArray) To UBound(lineArray)
stringColumn = stringRange.Column
fileInfo = Split(fieldsInfo(indexCount), ",")
stringRange.EntireRow.Cells(1, stringColumn).Value = Mid(lineArray(indexCount), CLng(fileInfo(0)), CLng(fileInfo(1)))
stringColumn = stringColumn + 1
Next indexCount
End Function
答案 0 :(得分:1)
这里有一个很好的教程:http://www.cpearson.com/excel/vbaarrays.htm
如果要将变量传递给函数,基本上使用Variant,您应该使用语法:
Private Function SplitColumns(ByRef lineArray As Variant, ByRef fieldsInfo As Variant, ByVal StartCell As Range) As Variant
要传递数组,您可以使用以下语法:
Private Function SplitColumns(ByRef lineArray() As String, ByRef fieldsInfo() As Double, ByVal StartCell As Range) As Variant
但你不能像这样传递一个变量:
ByRef lineArray() As Variant
祝你好运。