阵列VBS中的空白变量

时间:2013-12-17 16:08:14

标签: vba excel-vba vbscript excel

我是VBS的新手,我正在尝试从文本文件中收集的一些变量创建一个数组,但是当我尝试回显数组时,第一个变量为空,我不知道为什么我不知道VBS中有什么东西,我的代码如下

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "filepath"
archivedCSV = "filepath"
xmlFolder = "filepath"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in objFolder.Files
  if lcase(objFSO.GetExtensionName(objFile.Name)) = "csv" then
    origName = objFile.Name
    origPath = objFile.Path
    objFile.Name = "temp.txt"
    Set objFile = objFSO.OpenTextFile(objStartFolder & "\temp.txt", 1)
    Dim callOffNo()
    Dim orderNo()
    n = 0
    Do Until objFile.AtEndOfStream      
        strLine = objFile.ReadLine      
        arrFields = Split(strLine, ",") 
        If arrFields(0) <> "List of Call offs" Then
            If arrFields(0) <> "Call Off No" Then           
                If arrFields(2) <> currentOrder Then
                    currentOrder = arrFields(2)
                    ReDim orderNo(n)
                    orderNo(n) = arrFields(2)
                    n = n + 1
                End If          
            End If
        End If
    Loop    
    For Each order in orderNo
        MsgBox order
    Next
    objFile.Close
    newName = Replace(origName, objFSO.GetExtensionName(origPath), "")      
    archivedName = archivedCSV & "\" & origName
    tempFile = objStartFolder & "\temp.txt"     
    objFSO.MoveFile tempFile, archivedName  
  Exit for
 End if 
Next

txt文件看起来像这样

List of Call offs,,,,
Call Off No,Customer,Order Number,Item No,Quantity
12345,COMPANY,1013,1234,1
12345,COMPANY,1013,8652,1
12345,COMPANY,1013,4652,1
12345,COMPANY,1013,7203,1
12345,COMPANY,1013,3365,1
67891,COMPANY,1020,8963,2
67891,COMPANY,1020,1326,2

因此它应该向数组添加两个变量:1013,1020。但是第一个MsgBox是空白而第二个是1020

我确信这是一件我想念的简单事,

提前致谢

1 个答案:

答案 0 :(得分:1)

您需要Preserve来“增长”数组。一个'孤独'的ReDim创造了一系列空槽;这就是你的'先前'元素为空的原因。