Results 1 to 3 of 3

Thread: Simple problem... 4-column CSV file -> VB 4-column Array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    31

    Simple problem... 4-column CSV file -> VB 4-column Array

    Hi,
    I've got a simple question to most... but I've never used multiple-column arrays before (never could learn how to, mind-boggling!), and not really sure how to progress with this... not even sure if I should be using a multi-column array!

    The CSV is like this (even with linebreaks):
    392,3290,109
    310,1900,490
    390,1903,300
    321,1033,610

    and the CSV has 365 lines in it (one for every day of the year).

    I am trying to get the data into an array so it also has an index, like this:
    Code:
    Index, Cost, Units, Time
        1,  392,  3290,  109
        2,  310,  1900,  490
        3,  390,  1903,  300
        4,  321,  1033,  610
    This is the cost I use for importing the data into the program, and process into the array:

    In the FOR loop, I want to 'index' the CSV file..
    Code:
    Public Sub GetCostTimeRatio()
    
    Dim hFile As Long
    Dim LocalFileName As String
    
    Dim CostTimeRatioIndex(0 To 365, 0 To 365, 0 To 365, 0 To 365) As Long
    Dim CostTimeRatioString As String
    Dim CostTimeRatioLines() As String
    Dim n As Long
    
    LocalFileName = "Q:\timeandcost.csv"
          hFile = FreeFile
          Open LocalFileName For Input As #hFile
             CostTimeRatioString = Input$(LOF(hFile), hFile)
          Close #hFile
    
    CostTimeRatioLines = Split(CostTimeRatioString, vbCrLf)
    
    For n = 0 To UBound(CostTimeRatioLines)
        CostTimeRatioIndex(n,,,) = Split(CostTimeRatioLines(n), ",")
    Next
    
    Debug.Print CostTimeRatioLines(UBound(CostTimeRatioLines))
    End Sub
    But it says 'Can't assign to array' when I call the function, and it itemises the CostTimeRatioIndex as the culprit...

    Help.... please...

    Many thanks,

    Gabba
    Last edited by gabba; Nov 14th, 2006 at 08:03 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Simple problem... 4-column CSV file -> VB 4-column Array

    you can't use split to a multicolumn or fixed size array, you would need to write the values to the array in a loop
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Simple problem... 4-column CSV file -> VB 4-column Array

    Last edited by jeroen79; Nov 15th, 2006 at 01:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width