PC Review


Reply
Thread Tools Rate Thread

1d Variant arrays?

 
 
=?Utf-8?B?Sm9obiBLZWl0aA==?=
Guest
Posts: n/a
 
      12th Jun 2007
Can this be done?
....Create a 1d variant array from a range using an assignment directly to
the array like the 2d assignment statement?

'***test code***
Option Explicit
Option Base 1
Sub Array_Play()
Dim i As Integer, j As Integer, k As Integer
Dim vaData As Variant

'ThisWorkbook.Names.Add Name:="rngItemNo",
RefersTo:="=ProduceTotals!$A$6:$B$28" 'IS a 2d range
ThisWorkbook.Names.Add Name:="rngItemNo",
RefersTo:="=ProduceTotals!$A$6:$A$28" 'IS a 1d range

i = Range("rngItemNo").Rows.Count
j = Range("rngItemNo").Columns.Count

If j > 1 Then
'ReDim vaData(i, j) '<- handled automatically by the next stmt
vaData = Range("rngItemNo").Value
GoTo ArrayDone
End If

' still makes a 2d array... how do I code this to make vaData a 1d array
with out having to use the looping code below?
vaData = Range("rngItemNo").Value
Stop

' This loop loads the array as 1d, i'm looking for alternate ways to
accomplish this. Preferably a 1 line assigment statement.
ReDim vaData(i)
Dim c As Range
k = 0
For Each c In Range("rngItemNo").Cells
k = k + 1
vaData(k) = c.Value
Next c

ArrayDone:
Stop 'and look at the Locals
End Sub

--
Regards,
John
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9obiBLZWl0aA==?=
Guest
Posts: n/a
 
      12th Jun 2007
Just saw Tom's reply on another thread that answers this...

vaData = Application.Transpose(Range("rngItemNo").Value)

Thanks Tom!
--
Regards,
John


"John Keith" wrote:

> Can this be done?
> ...Create a 1d variant array from a range using an assignment directly to
> the array like the 2d assignment statement?
>
> '***test code***
> Option Explicit
> Option Base 1
> Sub Array_Play()
> Dim i As Integer, j As Integer, k As Integer
> Dim vaData As Variant
>
> 'ThisWorkbook.Names.Add Name:="rngItemNo",
> RefersTo:="=ProduceTotals!$A$6:$B$28" 'IS a 2d range
> ThisWorkbook.Names.Add Name:="rngItemNo",
> RefersTo:="=ProduceTotals!$A$6:$A$28" 'IS a 1d range
>
> i = Range("rngItemNo").Rows.Count
> j = Range("rngItemNo").Columns.Count
>
> If j > 1 Then
> 'ReDim vaData(i, j) '<- handled automatically by the next stmt
> vaData = Range("rngItemNo").Value
> GoTo ArrayDone
> End If
>
> ' still makes a 2d array... how do I code this to make vaData a 1d array
> with out having to use the looping code below?
> vaData = Range("rngItemNo").Value
> Stop
>
> ' This loop loads the array as 1d, i'm looking for alternate ways to
> accomplish this. Preferably a 1 line assigment statement.
> ReDim vaData(i)
> Dim c As Range
> k = 0
> For Each c In Range("rngItemNo").Cells
> k = k + 1
> vaData(k) = c.Value
> Next c
>
> ArrayDone:
> Stop 'and look at the Locals
> End Sub
>
> --
> Regards,
> John

 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      12th Jun 2007
One way:

Vertical:

Dim v As Variant
Dim i As Long
v = Application.Transpose(Range("A1:A10").Value)
For i = LBound(v) To UBound(v)
Debug.Print i, v(i)
Next i

Horizontal:

Dim v As Variant
Dim i As Long
With Application
v = .Transpose(.Transpose(Range("A1:J1").Value))
End With
For i = LBound(v) To UBound(v)
Debug.Print i, v(i)
Next i




In article <50A6B94B-EB85-4B64-8115-(E-Mail Removed)>,
John Keith <(E-Mail Removed)> wrote:

> Can this be done?
> ...Create a 1d variant array from a range using an assignment directly to
> the array like the 2d assignment statement?
>
> '***test code***
> Option Explicit
> Option Base 1
> Sub Array_Play()
> Dim i As Integer, j As Integer, k As Integer
> Dim vaData As Variant
>
> 'ThisWorkbook.Names.Add Name:="rngItemNo",
> RefersTo:="=ProduceTotals!$A$6:$B$28" 'IS a 2d range
> ThisWorkbook.Names.Add Name:="rngItemNo",
> RefersTo:="=ProduceTotals!$A$6:$A$28" 'IS a 1d range
>
> i = Range("rngItemNo").Rows.Count
> j = Range("rngItemNo").Columns.Count
>
> If j > 1 Then
> 'ReDim vaData(i, j) '<- handled automatically by the next stmt
> vaData = Range("rngItemNo").Value
> GoTo ArrayDone
> End If
>
> ' still makes a 2d array... how do I code this to make vaData a 1d array
> with out having to use the looping code below?
> vaData = Range("rngItemNo").Value
> Stop
>
> ' This loop loads the array as 1d, i'm looking for alternate ways to
> accomplish this. Preferably a 1 line assigment statement.
> ReDim vaData(i)
> Dim c As Range
> k = 0
> For Each c In Range("rngItemNo").Cells
> k = k + 1
> vaData(k) = c.Value
> Next c
>
> ArrayDone:
> Stop 'and look at the Locals
> End Sub

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      12th Jun 2007
John, the answer in the other thread was directed at you since you asked the
question in that thread as well. Here it is again for any others (although I
see J.E. McGimpsey has responsed here with an excellent answer).

For a single column Range
v = Application.Transpose(Range("G1:G10").Value)

v is a 1d array.

If the range were Horizonal (single row range), then you would need

v =Application.Transpose(Application.Transpose(Range("A1:M1").Value))

--
Regards,
Tom Ogilvy


"John Keith" wrote:

> Just saw Tom's reply on another thread that answers this...
>
> vaData = Application.Transpose(Range("rngItemNo").Value)
>
> Thanks Tom!
> --
> Regards,
> John
>
>
> "John Keith" wrote:
>
> > Can this be done?
> > ...Create a 1d variant array from a range using an assignment directly to
> > the array like the 2d assignment statement?
> >
> > '***test code***
> > Option Explicit
> > Option Base 1
> > Sub Array_Play()
> > Dim i As Integer, j As Integer, k As Integer
> > Dim vaData As Variant
> >
> > 'ThisWorkbook.Names.Add Name:="rngItemNo",
> > RefersTo:="=ProduceTotals!$A$6:$B$28" 'IS a 2d range
> > ThisWorkbook.Names.Add Name:="rngItemNo",
> > RefersTo:="=ProduceTotals!$A$6:$A$28" 'IS a 1d range
> >
> > i = Range("rngItemNo").Rows.Count
> > j = Range("rngItemNo").Columns.Count
> >
> > If j > 1 Then
> > 'ReDim vaData(i, j) '<- handled automatically by the next stmt
> > vaData = Range("rngItemNo").Value
> > GoTo ArrayDone
> > End If
> >
> > ' still makes a 2d array... how do I code this to make vaData a 1d array
> > with out having to use the looping code below?
> > vaData = Range("rngItemNo").Value
> > Stop
> >
> > ' This loop loads the array as 1d, i'm looking for alternate ways to
> > accomplish this. Preferably a 1 line assigment statement.
> > ReDim vaData(i)
> > Dim c As Range
> > k = 0
> > For Each c In Range("rngItemNo").Cells
> > k = k + 1
> > vaData(k) = c.Value
> > Next c
> >
> > ArrayDone:
> > Stop 'and look at the Locals
> > End Sub
> >
> > --
> > Regards,
> > John

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ReDim arrays in Variant Petr Danes Microsoft Excel Programming 8 11th Jun 2009 12:40 PM
Add variant arrays (without loop) =?Utf-8?B?QmhhcmF0aCBSYWphbWFuaQ==?= Microsoft Excel Programming 1 9th May 2007 12:46 PM
Marshalling Variant Arrays from COM =?Utf-8?B?QW5kcmV3IFMuIEdpbGVz?= Microsoft C# .NET 0 30th Mar 2005 10:36 PM
Passing variant arrays to C/C++ dll's agarwaldvk Microsoft Excel Programming 0 18th Oct 2004 01:33 AM
VB.NET variant arrays =?Utf-8?B?Um9iIEY=?= Microsoft VB .NET 2 26th Jan 2004 11:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:47 PM.