Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 11th, 2005, 10:09 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Transfer data from 1 array to another

Hi,

I'm trying to transfer selected data from 1 array to another, but I keep getting an error called "Subscript out of range". Below is my code. vaTest is the original array. vaSelect is the another array that should contain data from vaTest if the value is greater than 10. Does anyone have any ideas on how to get rid of the error?


Option Base 1
Sub test()

Dim vaTest As Variant
Dim vaSelect()

vaTest = Range("A3:A10").Value
j = 1
For i = 1 To UBound(vaTest, 1)
    If vaTest(i, 1) > 10 Then
        vaSelect(j, 1) = vaTest(i, 1)
        j = j + 1
    End If
Next i
End Sub


 
Old March 11th, 2005, 10:14 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

because the dimensions are not set in the declarations you are experiencing this error. try...

Dim vaSelect(10, 1)

cheers

Matthew

 
Old March 11th, 2005, 10:16 AM
Authorized User
 
Join Date: Feb 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,

u should try this:

Option Base 1
Sub test()

Dim vaTest As Variant
Dim vaSelect()
sheets(x).select '''x represents the sheet where you need to work on
'i think you cant use the Range("A3:A10").Value without first selecting the sheet of the ranges
vaTest = Range("A3:A10").Value
j = 1
For i = 1 To UBound(vaTest, 1)
    If vaTest(i, 1) > 10 Then
        vaSelect(j, 1) = vaTest(i, 1)
        j = j + 1
    End If
Next i
End Sub
 
Old March 11th, 2005, 11:41 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

If you know the array size you are going to need, Matthew’s answer will solve your problem. (You would still need to incorporate Matthew’s answer into maxpotters’ answer to avoid the error you’re getting, because you still would have an array with no dimensions or elements...)

(It looks as if you never use more than 1 dimension of vaSelect, so I am going to address this presuming a 1-dimension array is suitable.) If you do not know in advance what the size of the array needs to be, you can declare it as you have been doing, then immediately resize it to have 1 element
Code:
    Redim vaSelect(0)
Then, in your loop, repeatedly resize the array to accomodate what you are about to add to it, using the keyword ‘Preserve.’

When you resize an array, a different contiguous block of memory is allocated for the array. That is the end of the story if you do not specify ‘Preserve.’ But if you do specify ‘Preserve,’ then VB (and VBA) follows that step by copying all of the former data into the new block of memory before relinquishing its hold on the old memory.
Code:
j = 1
For i = 1 To UBound(vaTest, 1)
    If vaTest(i, 1) > 10 Then
        Redim Preserve vaSelect(j)
        vaSelect(j) = vaTest(i, 1)
        j = j + 1
    End If
Next i
If a couple of numbers get skipped because of the > 10 test, when the array is redimmed, the elements in between that last one that was filled in and the current one getting filled in will be initialized to the default value of the data type of the array. Since you have not specified a data type, the array is an array of Variants, so the interposing elements will be equal to Variant(Empty). If you specify the array as a numeric type they will equal zero.
 
Old March 14th, 2005, 02:02 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Guys -- I now have a better understanding of "Redim Preserve". My code now works.

I'm trying to increase my knowledge of arrays and so I'm experimenting with code. I know that its possible to have multi-dimensional arrays so I've altered my original code so that it now has two dimensions.

Basically what I'm trying to do is: I have a spreadsheet with 10 rows and 2 columns. I then want to transfer some of this data into an array where the contents in column A is greater than 10. As I don't know how many items will be greater than 10, I've used a dynamic array. I thought it would be a simple process of altering my original code by putting in a reference to the column number. But I get the "Subscript out of range" error when I try to Redim the array. I've read that the "Redim Preserve" doesn't allow you to change the number of columns, and so I'm confused as to how to "Redim Preserve" an array to increase the number of rows, without specifying the column index.

Hopefully someone can clarify this for me. Below is my altered code:

Option Base 1
Sub test()

Dim vaTest As Variant
Dim vaSelect()

Worksheets("Sheet1").Select
vaTest = Worksheets("Sheet1").Range("A1:B10")

j = 1
For i = 1 To UBound(vaTest, 1)
    If vaTest(i, 1) > 10 Then

        ReDim Preserve vaSelect(j, 2)

        vaSelect(j, 1) = vaTest(i, 1)
        vaSelect(j, 2) = vaTest(i, 2)
        j = j + 1
    End If
Next i
End Sub

 
Old March 14th, 2005, 06:20 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can only increase the size of an array using the Preserve keyword if you are re-sizing the last dimension of that array.

If you have VB Help installed, place your cursor on the Preserve keyword in the VB editor and press the F1 key. You'll find there a decent explanation of how this works.





Similar Threads
Thread Thread Starter Forum Replies Last Post
DATA TRANSFER vish_vj ADO.NET 1 March 7th, 2008 01:16 PM
Data Transfer problem in C# akumarp2p C# 0 December 5th, 2006 10:38 AM
Transfer double array from asp to javascript simi Classic ASP Databases 1 May 17th, 2005 06:40 AM
Transfer data mepancha SQL Server 2000 4 March 24th, 2005 04:29 PM
Data Transfer psingh SQL Server 2000 8 June 9th, 2003 06:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.