Consulting

Results 1 to 4 of 4

Thread: WSH: Multidimensional dynamic arrays. Simple demonstration code included.

  1. #1

    WSH: Multidimensional dynamic arrays. Simple demonstration code included.

    I am stuck trying to create a multidimensional dynamic array in VBS. I cannot find a clue to figure out how to do it. How do you type an array to make one or more of the dimensions dynamic? Simply using "Dim MyArray() works for only one dimension, at least in the code below. Try the sample code below. The text file needs to be in the same directory you put your WSH file.

    This is the ColArrayTEST.txt file:
    Col 1 - Row 1; Col 2 - Row 1; Col 3 - Row 1
    Col 1 - Row 2; Col 2 - Row 2; Col 3 - Row 2
    Col 1 - Row 3; Col 2 - Row 3; Col 3 - Row 3
    Col 1 - Row 4; Col 2 - Row 4; Col 3 - Row 4
    Col 1 - Row 5; Col 2 - Row 5; Col 3 - Row 5
    Col 1 - Row 6; Col 2 - Row 6; Col 3 - Row 6
    Col 1 - Row 7; Col 2 - Row 7; Col 3 - Row 7
    And this is the VBScript file:
    ' VB Script Document
     
    Option Explicit
     
    Dim x
    Dim objFSO
    Dim objTextFile, objOutPut, SplitOutPut
     
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objOutPut = objFSO.OpenTextFile("ColArrayTEST.txt")
     
    Dim OutPut(7, 3)
    'Dim OutPut() 'THIS DOES NOT WORK!  HOW DO YOU MAKE THAT "x" DIMENSION BELOW DYNAMIC? 
     
    x= 0
     
    Do  Until objOutPut.AtEndOfStream
     SplitOutPut = Split(objOutPut.ReadLine, ";")
     OutPut(2, x) = SplitOutPut(2)
     OutPut(0, x) = SplitOutPut(0)
     OutPut(1, x) = SplitOutPut(1)
     x = x + 1
    Loop
     
    Wscript.echo("Array Sample:  " & OutPut(3, 0) & ", " & OutPut(3, 1) & ", " & OutPut(3, 2))
    If you use "Dim OutPut()", you get an error, "Subscript out of range" for the line "OutPut(2, x) = SplitOutPut(2)". This is the place the compiler first encounters the second dimension.

    I am completely stumped. Any and all tips or clues would be appreciated.

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Have a look here for some information that may be helpful.

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    None of the cases at that post really address my problem as near as I can tell. The code there assumes that the size of either of the dimensions are always known. I need one dimension fixed to 3 and the other dynamic. We assume in advance in this code that "... Until objOutPut.AtEndOfStream" is not known in advance. We would like to apply a list of any length. As far as I can tell there is no property available for TextStream to find out how many lines of the list that "ReadLine" will encounter.

  4. #4
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    SM,
    I've noted the changes I made, the ReDim Preserve line is really the key change though:[vba]Option Explicit

    Dim x
    Dim objFSO
    Dim objTextFile, objOutPut, SplitOutPut

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objOutPut = objFSO.OpenTextFile("ColArrayTEST.txt")

    Dim OutPut() '***** REMOVED THE FIXED DIMENSIONS

    x= 0

    Do Until objOutPut.AtEndOfStream
    ReDim Preserve OutPut(2, x) '***** ADDED THIS LINE!
    SplitOutPut = Split(objOutPut.ReadLine, ";")
    OutPut(2, x) = SplitOutPut(2)
    OutPut(0, x) = SplitOutPut(0)
    OutPut(1, x) = SplitOutPut(1)
    x = x + 1
    Loop

    objOutPut.Close '***** NEED TO CLOSE FILE!
    Set objFSO = Nothing '***** SETTING VARIANT NON-ARRAY VARIABLES TO NOTHING
    Set objOutPut = Nothing
    Set objTextFile = Nothing 'YOU HAVENT EVEN USED THIS VARIABLE YET
    set SplitOutPut = Nothing
    '***** SWITCHED DIMENSIONS IN NEXT LINE FROM 3,0 TO 0,3, ETC
    Wscript.echo("Array Sample: " & OutPut(0, 3) & ", " & OutPut(1, 3) & ", " & OutPut(2, 3))[/vba]Matt

Posting Permissions

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