Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA, arrays and Consolidate

Posted on 1999-07-28
7
Medium Priority
?
2,258 Views
Last Modified: 2012-06-27
Hi guys... maybe this is an easy one.

I have this Macro in Excel VBA and it works:

-----------------------------------------------------------
Selection.Consolidate Sources:=  _
    Array("'Consolidado'!R10C1:R16C3", _
          "'Consolidado'!R18C1:R27C4") _
    , Function:=xlSum, TopRow:=False, LeftColumn:=True, _
    CreateLinks:=False
-----------------------------------------------------------

The "consolidate" command needs an array to especify the ranges to be consolidated. In this example there's an array of 2 elements.

And this is my problem: In runtime the array can be 1, 2, 3 o more elements, and I don't know how to build the array (in runtime of course). The "consolidate" command doesn't let me put an string array.


Thank you guys.

0
Comment
Question by:nfernand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 13

Accepted Solution

by:
cri earned 400 total points
ID: 1610305
nfernand,
Here a copy and paste of the VBA help:

****begin****
Declaring a Dynamic Array

By declaring a dynamic array, you can size the array while the code is running. Use a Static, Dim, Private, or Public statement to declare an array, leaving the parentheses empty, as shown in the following example.

Dim sngArray() As Single

Note   You can use the ReDim statement to declare an array implicitly within a procedure. Be careful not to misspell the name of the array when you use the ReDim statement. Even if the Option Explicit statement is included in the module, a second array will be created.

In a procedure within the array's scope, use the ReDim statement to change the number of dimensions, to define the number of elements, and to define the upper and lower bounds for each dimension. You can use the ReDim statement to change the dynamic array as often as necessary. However, each time you do this, the existing values in the array are lost. Use ReDim Preserve to expand an array while preserving existing values in the array. For example, the following statement enlarges the array varArray by 10 elements without losing the current values of the original elements.

ReDim Preserve varArray(UBound(varArray) + 10)

Note   When you use the Preserve keyword with a dynamic array, you can change only the upper bound of the last dimension, but you can't change the number of dimensions.

****end****

Hope this helps. Otherwise please ask back before rejecting. Thanks.
\cri
0
 
LVL 5

Expert Comment

by:vboukhar
ID: 1610306
nfernand!
It's easy indeed - see my sample below:

Sub ChangeSource()
Dim DimRan As Variant
DimRan = Array("d1!R1C1:R5C2", "d2!R1C1:R5C2")
Worksheets(1).Range("A1").Consolidate _
    Sources:=DimRan, _
    Function:=xlSum
' now consolidate data from three sheets:
DimRan = Array("d1!R1C1:R5C2", "d2!R1C1:R5C2", "d3!R1C1:R5C2")
Worksheets(1).Range("A1").Consolidate _
    Sources:=DimRan, _
    Function:=xlSum
End Sub

By the way, you don't need to use ReDim in this case - if you define variable as Valiant, it could contains reference to array of any dimentions. You have to use ReDim to change the number of dimentions of previously explicitly defined array:
Dim a(3) as Variant
..
ReDim a(5)

Hope it helps.
0
 
LVL 2

Author Comment

by:nfernand
ID: 1610307
Hi Cri and thank you for your answer.

As you ask me, I didn't reject your answer if it didn't works, and it didn't. According to your text I wrote this:
-----------------------------------------------
Dim vector() As string
ReDim vector(1)
vector(1) = "'Consolidado'!R10C1:R16C3"
ReDim Preserve vector(UBound(vector) + 1)
vector(2) = "'Consolidado'!R18C1:R27C4"
Range("F10").Select
Selection.Consolidate Sources:=vector _
  , Function:=xlSum, TopRow:=False, _
  LeftColumn:=True, CreateLinks:=False
------------------------------------------------
I understand that "redim" can changes an array size, and it do it, but "consolidate" still gives me the same error: 1004 "Not a valid consolidation reference"




Mr. vbokhar

Thank you for your answer but it doens't look too useful, because I don't know how many elements the array may have.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Expert Comment

by:cri
ID: 1610308
nfernand,
Thank you for giving me the second chance I asked for. It is getting late, and I do not want to lock this question longer:

a) Why do you define the array as _string_ ? You pass the ranges as string, yes, but what do you want to consolidate ? I would expect as Single, Long, Currency, etc. or as Variant.

b) I think you are misusing the preserve command, see 'Note' in the help text of my first post.  

I think vboukhar is on the right track and he knows more about VBA than I do. So if the a.m. did not help, reject my answer and continue with him. I bet you are finished before the weekend.

Best Regards
\cri
0
 
LVL 5

Expert Comment

by:vboukhar
ID: 1610309
nfernand!
Look at little changed code:

Sub ChangeSource()
Dim DimRan As Variant
DimRan = Array("d1!R1C1:R5C2", "d2!R1C1:R5C2")
Worksheets(1).Range("A1").Consolidate _
    Sources:=DimRan, _
    Function:=xlSum
' now consolidate data from three sheets:
ReDim Preserve DimRan(UBound(DimRan)) ' increase size
DimRan(UBound(DimRan) - 1) = "d3!R1C1:R5C2" ' add new item
Worksheets(1).Range("A1").Consolidate _
    Sources:=DimRan, _
    Function:=xlSum
End Sub

It's same thing, but with ReDim Preserve instead of redifinition of Array (in my previous comment). I meant, that if you create string with address of consolidated ranges in run-time, you can use this string either in Array definition or in ReDim and next assigment of added item of array.
I hope it helps.
0
 
LVL 22

Expert Comment

by:ture
ID: 1610310
nfernand,

1. The variable that should hold your array should be declared as a variant, not as a string.

2. The first element of an array is numbered 0 by default.
Redim Vector(1) actually makes the array two elements large. Vector(0) and Vector(1)

3. You don't need to use UBound if you know the number yourself.

Sub Helpnfernand()
  Dim Vector
  ReDim Vector(0)
  Vector(0) = "'Consolidado'!R10C1:R16C3"
  ReDim Preserve Vector(1)
  Vector(1) = "'Consolidado'!R18C1:R27C4"
  Selection.Consolidate _
    Sources:=Vector, _
    Function:=xlSum, _
    TopRow:=False, _
    LeftColumn:=True, _
    CreateLinks:=False
End Sub

Ture Magnusson
Karlstad, Sweden
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6784347
You awarded this question to cri, but cri never got these points until I fix them today.  This question has been stuck in a -1 userid condition and I found it and will fix this today.  The expert will finally get these points now and the well deserved "A" grade.  These problems happened in 1999 when we were having the database and jsp-500 errors.  All fixed and your points verified.


Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question