如何从一个指定地址的TXT文本中读出数据并且按要求把这些数据放入相应的数组例如:ITEM1:   1,2,3......
ITEM2:   2,4,5......
......把第一行ITEM1后的数字放入数组a()
把第二行ITEM2后的数字放入数组b()实际问题的要求是
TXT文本是一可由客户自由修改的
item1,item2.....是作为combobox.text的初始值
而后面所跟数据是分别在item1等条件下需要放入相应的数组的数据
也就是说item1...是做为条件由客户自己设定,数据也自己设定
所以至于item到底是何字符未知,后面所跟数据个数未知,行数未知我在不得已的情况下用了这个笨方法,预先设定足够多的数组来应付多出来的行数已经为这个烦了1天了。。
急啊 谁能帮个忙

解决方案 »

  1.   

    Private Sub Command1_Click()
        Dim stringStr As String
        Dim arrayList() As String
        Dim i As Integer
        stringStr = "1,2,3,4,5,6"
        arrayList() = Split(stringStr, ",")
        For i = LBound(arrayList) To UBound(arrayList)
            Debug.Print arrayList(i)
        Next
    End Sub
      

  2.   

    “item到底是何字符未知”
    ================
    解决办法:读入一行,查找“:”,“:”前的字符就是Item.
      

  3.   

    试试行不行?Sub ReadFiles
       Dim fso As String , ts As String , s As String , sPath As String 
       Dim m As Integer, n As Integer
       Dim a() as String 
       m = combobox.Text
       n = m - 1
       Const ForReading = 1
       Set fso = CreateObject("Scripting.FileSystemObject")
       ' 读取文件的内容。
       Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
       s = ts.ReadAll
       '读取每一行
       a = Split(s, vbCrLf)
       If n > UBound(a) Then
       MsgBox ("not exist ")
       ElseIf n <= UBound(a) Then
       sPath = Right(a(n), Len(a(n)) - 6) '其中的6代表每个具体语句即1,2,3......前面的字符数
       End If
       ts.Close
    End Sub
      

  4.   

    Private Function CountLines(FilePath As String) As Long
    '****     计算文本文件行数    ****
    '**** by VBDN [email protected] ****
        Dim FileNumber As Integer
        Dim TempStr As String
        Dim TempCount As Long
        FileNumber = FreeFile
        Open FilePath For Input As #FileNumber
        Do While Not EOF(FileNumber)
            Line Input #FileNumber, TempStr
            TempCount = TempCount + 1
        Loop
        Close #FileNumber
        CountLines = TempCount
    End FunctionPrivate Sub Command1_Click()
        Debug.Print CountLines("C:\CountLines.txt")
    End Sub
      

  5.   

    Dim myString As String = "Look at these!"
    ' Returns ["Look", "at", "these!"]
    Dim myArray() As String = Split(myString)
    然后就可以按照数组赋值了
      

  6.   

    Try
                If FwTxtStrDBC1.Text = "" Then
                    MsgBox("文件未选择")
                End If
                Dim sr As StreamReader = New StreamReader(FwTxtStrDBC1.Text)
                Dim str As String
                Dim strSplit()
                Dim intRowNo As Integer = 1
                Dim i As Integer
                Dim file As New System.IO.StreamWriter("c:\test.txt", True)
                Do While sr.Peek() >= 0
                    str = sr.ReadLine()
                    strSplit = Split(str, ",")
                    strConsignment_no = strSplit(0)
                    strClt_AcctID = strSplit(1)
                    strClt_Name = strSplit(2)
    .............................................
    以此类推
      Loop
                file.Close()
                sr.Close()
    '一定要给我分
      

  7.   

    我觉得还是自己简单化后反而偏离了我的需求
    真是越来越笨了我步骤是这样的
    用户在txt中输入内容(C:\test.txt)
    程序读取txt
    将每行第一个字符串(如ITEM1,ITEM2……)放入strArray(),然后放入ComboBox.Text
      '我的想法是  for 语句 AddItem.strArarry()
    对每行:后的数据转化成Integer分别放入a1(),a2()……分一定给,加分都行,只希望能解决我的难题
    现在头都大了
      

  8.   

    可以用INSTR和RIGHT LEFT字符串处理函数做
    需要一些很精巧的思路