+ Reply to Thread
Results 1 to 8 of 8

Dynamic Array

Hybrid View

  1. #1
    Registered User
    Join Date
    06-10-2011
    Location
    Tartu, Estonia
    MS-Off Ver
    Excel 2000
    Posts
    7

    Dynamic Array

    I need a 2-dimensinal dynamic array, but the problem is, that I don't have the 1st dimension as fixed number. It is read from Named Range instead. It doesnt change after the array is declared for the scope of procedure, but it may be initialized at start, when according cell is empty.

    I have a sample of data, where an indetermined number of articles are delivered at certain dates (weekly or monthly). The maximal possible number of different weeks is stored in worksheet cell, which is defined as Named Range, p.e. MaxWeeks. I need to read those data into array p.e. arrData(1 To MaxWeeks+2, 1 To n), where arrData(1, n) is quantity of article n for 1st week, ..., arrData(MaxWeeks, n) is quantity of article n for last week, arrData(MaxWeeks+1, n) is article number, and arrData(MaxWeeks+2, n) is total quantity of article n.

    The idea is to declare the dynamic array, and the use Redim Preserve to add new row for every new article. But I haven't found a way to make VBA to accept MaxWeeks as a constant (and because I may need to initialize MaxWeeks, I can't declare it in declarations section anyway), and I can't use Redim to add a new dimension.

    Is there some solution?
    Thanks in advance!

    Arvi Laanemets

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Dynamic Array

    Redim Preserve only preserves the last dimension of an array - i.e. n rather than MaxWeeks.

  3. #3
    Registered User
    Join Date
    06-10-2011
    Location
    Tartu, Estonia
    MS-Off Ver
    Excel 2000
    Posts
    7

    Re: Dynamic Array

    Quote Originally Posted by StephenR View Post
    Redim Preserve only preserves the last dimension of an array - i.e. n rather than MaxWeeks.
    I know!

  4. #4
    Registered User
    Join Date
    06-10-2011
    Location
    Tartu, Estonia
    MS-Off Ver
    Excel 2000
    Posts
    7

    Re: Dynamic Array

    The solution was relatively easy - it looks like the summer heat did get me
    ...
    Dim arrData() As Variant
    ReDim arrData(MaxWeeks + 2)
    ...
    ReDim arrData(iMaxWeeks + 2, n)
    where n will be 1 To N
    Last edited by ArviL; 06-11-2011 at 01:43 PM.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Dynamic Array

    Why don't you use:

    arrdata=sheets(1).cells(1).currentregion



  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,310

    Re: Dynamic Array

    Hi ArviL and welcome to the forum,

    It looks to me you want a Dynamic Named Range. See
    http://www.ozgrid.com/Excel/DynamicRanges.htm or
    http://www.vertex42.com/ExcelArticle...ed-ranges.html
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  7. #7
    Registered User
    Join Date
    06-10-2011
    Location
    Tartu, Estonia
    MS-Off Ver
    Excel 2000
    Posts
    7

    Re: Dynamic Array

    Quote Originally Posted by MarvinP View Post
    It looks to me you want a Dynamic Named Range.
    I use various dynamic ranges frequently, but they aren't applicaple for current task.
    I have 4 different source tables in separate files (either Excel workbooks or text files) - everyone with different data structure, with different delivery frequency and with different delivery scope from couple of months up to year. And the result will be a text file applicable for data import into 3rd party program. The Excel workbook, where the VBA procedure is started, acts as go-between - it reads data from source tables, makes all necessary calculations, and writes results into text file - without writing a single bit of data into some of its own worksheets.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,310

    Re: Dynamic Array

    Ok - So Dynamic Named Ranges don't do it for you.

    How about VBA code that finds the last row and last column data is in to define a range.

    The last row that data is in, using column A can be found by this:
    Dim LastRow as Double
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    The last column data is in using Row 1 is given by this
    Dim LastCol as Double
    LastCol = Cells(1. Columns.Count).End(xlToLeft).Column
    Read
    http://vbautomation.110mb.com/VBA/wo..._worksheet.htm

    hope that helps

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1