Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Writing a range to an array...

Hello

How can I use VBA to write a range to an array, change the elements of the
array, and then write back to a worksheet?

I started with this but got an error for the line Data(i)...

Sub ArrayTest()
Dim Data As Variant, i As Integer

Data = Range("A1:A10").Value
For i = LBound(Data) To UBound(Data)
Data(i) = Data(i) + 10
Next i
End Sub

Here I want to take range A1:A10, add 10 to all of the values, and the write
it back to the worksheet.

I want to do this as I think it is faster then maipulating individual cells
in a range, especially when the range is large.

Any help very welcome.

Regards


Alex
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Writing a range to an array...

Alex,
Try this:

Sub ArrayTest()
Dim Data As Variant, i As Integer

Data = Range("A1:A10")
For i = LBound(Data) To UBound(Data)
Data(i, 1) = Data(i, 1) + 10
Next i
Range("A1:A10") = Data
End Sub

HTH


"Alex" wrote:

Hello

How can I use VBA to write a range to an array, change the elements of the
array, and then write back to a worksheet?

I started with this but got an error for the line Data(i)...

Sub ArrayTest()
Dim Data As Variant, i As Integer

Data = Range("A1:A10").Value
For i = LBound(Data) To UBound(Data)
Data(i) = Data(i) + 10
Next i
End Sub

Here I want to take range A1:A10, add 10 to all of the values, and the write
it back to the worksheet.

I want to do this as I think it is faster then maipulating individual cells
in a range, especially when the range is large.

Any help very welcome.

Regards


Alex

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Writing a range to an array...

Just to add to Toppers excellent suggestion,

Any array formed in this manner will always be a 2D array. Also, it will
always be 1 based regardless of the option base setting.

Dim v as Variant
v = Range(a1:A10).Value is v(1 to 10, 1 to 1)
v = Range(A1:J1).Value is v(1 to 1, 1 to 10)
and of course

v = Range(A1:J10).Value is v(1 to 10, 1 to 10)

--
Regards,
Tom Ogilvy



"Toppers" wrote in message
...
Alex,
Try this:

Sub ArrayTest()
Dim Data As Variant, i As Integer

Data = Range("A1:A10")
For i = LBound(Data) To UBound(Data)
Data(i, 1) = Data(i, 1) + 10
Next i
Range("A1:A10") = Data
End Sub

HTH


"Alex" wrote:

Hello

How can I use VBA to write a range to an array, change the elements of

the
array, and then write back to a worksheet?

I started with this but got an error for the line Data(i)...

Sub ArrayTest()
Dim Data As Variant, i As Integer

Data = Range("A1:A10").Value
For i = LBound(Data) To UBound(Data)
Data(i) = Data(i) + 10
Next i
End Sub

Here I want to take range A1:A10, add 10 to all of the values, and the

write
it back to the worksheet.

I want to do this as I think it is faster then maipulating individual

cells
in a range, especially when the range is large.

Any help very welcome.

Regards


Alex



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing to An Array or Collection vqthomf Excel Programming 4 August 23rd 05 02:07 PM
Q: Best way to take data from VBA into graphs without writing data to worksheets? (Can a named range refer to an array in memory only?) KR Excel Programming 2 December 16th 04 11:12 PM
Writing Range to Array Marston Excel Programming 3 August 9th 04 09:11 PM
Prb: Writing Array functions in VBA neebington Excel Programming 2 March 3rd 04 03:00 PM
Writing Array Formulas in VBA Ed Excel Programming 4 January 10th 04 07:27 PM


All times are GMT +1. The time now is 11:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"