Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Speeding up VBA array program 1

Status
Not open for further replies.

ngerman01

Programmer
Oct 2, 2003
10
US
Right now i have a program that that collect 2 columns of data out of excel and puts them into an array. The way i am doing this is as follows
Range("B1").Select
For I = 0 To 16000
pH(I, 0) = ActiveCell
pH(I, 2) = ActiveCell.Offset(0, 1)
ActiveCell.Offset(1, 0).Select
Next I

Now this works fine but it has to run thru all 16 thousand rows. So it takes about 45 seconds. The rest of the program runs thru the array, in VBA, and can do it in about 2 second(if that). I was wondering if there is any way to do something as follows.

var1 = "B1"
var2 = "B2"
For I = 0 To 16000
pH(I, 0) = Range(var1)
pH(I, 2) = Range(var2)
var1 = var1 + 1
var2 = var2 + 2
Next I

Hopefully by doing this it would not have to scroll thru all 16,000 rows.
 
If you can re-structure your array to map the columns (i.e., columns B and C into ,0 and ,1) then this works practically instantaneously:
[blue]
Code:
Sub test()
Dim MyData As Variant
  MyData = Range("B1:C16000")
  MsgBox MyData(15999, 2)
End Sub
[/color]

You don't actually have to work with arrays as such. You can use a variant for an array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top