Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Read and write an array

This is probably pretty simple but I'm new to reading and writing arrays. I
need to use a for next loop to read values from cells A1 through A4 for
example. Then i need it to write those values to cells to B1 through B4. I'm
sure it's a cake walk, but again, I'm new at some of this stuff.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Read and write an array

A cake walk? No, but close...

Range("B1:B4").Value = Range("A1:A4").Value
--
Jim Cone
Portland, Oregon USA


"Dave L"
wrote in message
This is probably pretty simple but I'm new to reading and writing arrays. I
need to use a for next loop to read values from cells A1 through A4 for
example. Then i need it to write those values to cells to B1 through B4. I'm
sure it's a cake walk, but again, I'm new at some of this stuff.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Read and write an array

Thanks Jim. That works for some of the simpler things I need to do, but I was
hoping to use a loop to grab each cells' value separately and then write it
separately. In other words I want to select A1 and read the value into the
array, select A2 and read the value into the array, etc. Then I want to move
to B1 and write the first value, B2 and write the second, etc.

I could explain the whole scope of the project, but there will be way too
many variations on the scenario above. I'm really just looking for the basic
concept of a for next loop and then I can take it from there.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Read and write an array


Be careful what you wish for. <g
'--

Sub OneByOneWithRanges()
Dim rngFirst As Range
Dim rngOther As Range
Dim N As Long

Set rngFirst = Range("A1:A4").Cells
Set rngOther = Range("C10:F10").Cells

For N = 1 To rngFirst.Count
rngOther(N).Value = rngFirst(N).Value
Next
End Sub
'--

Sub OneByOneWithArrayAndRange()
Dim vFirst As Variant
Dim rngOther As Range
Dim N As Long

'A variant containing an array.
vFirst = Range("A1:A4").Value
Set rngOther = Range("C10:F10").Cells

'Transfer array values to other range
For N = 1 To UBound(vFirst)
rngOther(N).Value = vFirst(N, 1)
Next
End Sub
'--

Sub UseTwoArrays()
Dim vFirst As Variant
Dim vOther() As Variant
Dim M As Long
Dim N As Long
Dim i As Long
Dim j As Long

vFirst = Range("A1:A4").Value
i = UBound(vFirst, 1)
j = UBound(vFirst, 2)

' Transpose array size - columns to rows and rows to columns.
ReDim vOther(1 To j, 1 To i)

' Add values from 1st array to 2nd array
' Note (N, M) vs. (M, N)
For M = 1 To i
For N = 1 To j
vOther(N, M) = vFirst(M, N)
Next
Next

' Add 2nd array values to range.
Range("C10:F10").Value = vOther()
End Sub
--
Jim Cone
Portland, Oregon USA




"Dave L"
wrote in message
Thanks Jim. That works for some of the simpler things I need to do, but I was
hoping to use a loop to grab each cells' value separately and then write it
separately. In other words I want to select A1 and read the value into the
array, select A2 and read the value into the array, etc. Then I want to move
to B1 and write the first value, B2 and write the second, etc.

I could explain the whole scope of the project, but there will be way too
many variations on the scenario above. I'm really just looking for the basic
concept of a for next loop and then I can take it from there.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Read and write an array

Thanks again Jim! I think I can make this work. I have one more question and
I'll try to stop bugging you. I need to ignore empty cells in my range. In
other words if I have values in all cells in range A1:A4 except for A3, I
need to write cells B1:B3 with the values from A1, A2 and A4. Is that easy or
should I just write a clean up script to get rid of blank values after
they're written?


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Read and write an array

I can't answer your question as it is difficult to see your script from here.
You can check for blank cells with...

If Len(rCell.Value) = 0 Then
-or-
If IsEmpty(rCell) Then

Checking for "blank" array elements depends on the data type of the array.
Numeric array elements are 0.
Object array elements are "Nothing"
String array elements are ""
Variant array elements are "Empty"
--
Jim Cone




"Dave L"
wrote in message
Thanks again Jim! I think I can make this work. I have one more question and
I'll try to stop bugging you. I need to ignore empty cells in my range. In
other words if I have values in all cells in range A1:A4 except for A3, I
need to write cells B1:B3 with the values from A1, A2 and A4. Is that easy or
should I just write a clean up script to get rid of blank values after
they're written?
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
Array to read data from one sheet and write to another if it meets criteria RudyShoe Excel Programming 2 August 2nd 06 04:38 PM
I have a read only xl file, I need it to be read and write drama queen Excel Discussion (Misc queries) 3 July 1st 06 12:25 AM
Read/Write mattsvai[_14_] Excel Programming 4 March 30th 06 01:55 AM
How can a file be converted from Read-Only to Read/Write Jim in Apopka Excel Discussion (Misc queries) 2 November 19th 05 04:59 PM
Who has read/write John Pritchard Excel Programming 0 September 28th 04 04:36 PM


All times are GMT +1. The time now is 02:19 AM.

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

About Us

"It's about Microsoft Excel"