Transferring an array to multiple ranges - arrays

I have become stuck trying to solve a piece of code I thought would be very simple.
I have defined a range (2 rows, 150 columns) and transferred it to an array. I then want to use the defined array in multiple ranges (same size of 2 rows and 150 columns). I have written the following code:
Dim LocalArray As Variant
LocalArray = .Range("FD6781:KW6782").Value2
.Range("FD6839:KW6840,FD6955:KW6956,FD7013:KW7014,FD7071:KW7072").Value2 = LocalArray
The issue is that every second range defined in .range("FD6839:KW6840,FD6955:KW6956...") shows up as N/A. Hence range FD6839:KW6840 is correct while range FD6955:KW6956 is wrong.
What have I done wrong in the above code?
Thank you!

You could loop. Otherwise, it does seem to be related to the number of columns causing the issue. Seems odd.
Option Explicit
Public Sub test()
Dim localArray(), rng As Range, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet3")
localArray = ws.Range("FD6781:KW6782").Value2
For Each rng In ws.Range("FD6839, FD6955, FD7013, FD7071")
rng.Resize(UBound(localArray, 1), UBound(localArray, 2)) = localArray
Next
End Sub

Related

Writing Array Back to Spreadsheet at Multiple Different Places

I am attempting to simplify my code when writing back data from an array into an Excel spreadsheet.
I have an array of 2 rows and 49 columns (the data is dates used for the horizontal part in a number of graphs). The date's adapt based on user-input and it is then written back into the Excel spreadsheet.
Currently I have written below code for loading the data into the array and writing it back into the spreadsheet (and it works as intended).
Dim LocalArray() As Variant
LocalArray = Sheets("Data").Range("L4003:BH4004").Value2
.Range("M280").Resize(UBound(LocalArray, 1), UBound(LocalArray, 2)) = LocalArray
.Range("M336").Resize(UBound(LocalArray, 1), UBound(LocalArray, 2)) = LocalArray
.Range("M394").Resize(UBound(LocalArray, 1), UBound(LocalArray, 2)) = LocalArray
Above is only part of the code though as I need to include the array in 15 different places (graphs). Hence I repeat the same line of code a lot of times which seems very ineffecient.
I have tried using below simple line of code to write the array data back into the spreadsheet:
.Range("M280:BI281,M336:BI337,M394:BI395").Value2 = LocalArray
However, writing back the array data using above code makes every second range appear wrong with cells including N/A (ref. below picture).
How do I write this code the simplest way possible (and requiring as little processing power from the user's PC as possible)?
Thank you very much!
It is much easier to examine and remember the dimensions of the source range.
Say we have data in a rectangular set of cells from B2 through D3:
That we wish to copy elsewhere several times. The first time will be a block starting at E6:
Sub dural()
Dim LocalArray() As Variant
Dim rng As Range, rw As Long, cl As Long
Set rng = Sheets("Sheet1").Range("B2:D4")
rw = rng.Rows.Count
cl = rng.Columns.Count
LocalArray = rng.Value2
Range("E6").Resize(rw, cl) = LocalArray
End Sub
Running this yields:
So all we need to remember is rw and cl.
EDIT#1:
For an easy way to loop the deposits:
Sub dural()
Dim LocalArray() As Variant
Dim rng As Range, rw As Long, cl As Long
Dim a
Set rng = Sheets("Sheet1").Range("B2:D4")
rw = rng.Rows.Count
cl = rng.Columns.Count
LocalArray = rng.Value2
Range("E6").Resize(rw, cl) = LocalArray
' Now try looping
For Each a In Array("a12", "b16", "c23")
Range(a).Resize(rw, cl) = LocalArray
Next a
End Sub

Assigning a named range to array

I've found something that I don't quite understand. Please help explain.
so I used this code to assign values within named ranges to an array. (see below).
Dim accountStructure As Variant
ReDim accountStructure(2)
accountStructure(0) = Range("namedrange1")
accountStructure(1) = Range("namedrange2")
accountStructure(2) = Range("namedrange3")
The code works. However, in order to call the first item in accountstructure(0) I need to type accountStructure(0)(1,1), second item accountStructure(0)(2,1) third item (0)(3,1) and so on.
I understand why there is (0) there but I don't understand why there is always a 1 after the item number. Please help me understand this.
If we have any range
Dim rng as Range
Set rng=Range("A1:A10")
We can assign
Dim arr As Variant
arr = rng
But we have to treat it as two dimensional array. Because Excel VBA does'nt "know" it is one dimensional.
So we can now
For i=1 To 10
arr(i,1) = i
Next

Populate an array without a loop

I am trying to avoid the use of loops for populating arrays since they take a lot of time when managing a lot of data.
Apparently as well, that is possible and easy in VBA but often results in problems.
Here is the code:
sub populate()
'put the whole column in an array
Dim AppArray() As Variant
Dim AppRange As Range
'calculate the last row of the column 1 of sheets
Dim LstRow As Integer
LstRow = Sheets("whole").Cells(Sheets("whole").Rows.Count, "A").End(xlUp).row
'here I calculate the range that I want to pass to the array
Set AppRange = Sheets("whole").Range(Cells(1, 1), Cells(LstRow, 1))
MsgBox ("apprange " & AppRange.Address)
'i dont know if I need to redim or not
ReDim AppArray(1 To LstRow)
'here comes the point. populate the array with the values of the range
AppArray = AppRange.Value
End Sub
This does not work. I also tried application.tranpose(AppRange.Value).
I used:
For i = 1 To LstRow
Debug.Print AppArray(i)
Next
and an error appears, so somehow there is no AppArray(1).
I would be very happy if you can comment on that. More than just arranging the code suggest even other pages (links) to populate arrays with values of ranges when these ranges are not known in advance.
If the case is that looping is very time consuming and that arrays can be populated straight away, I don't understand why 99% of the pages referring to arrays use a loop (or nested loop) to populate an array.
I found the answer.
dim myRange as range
dim myArray() as variant
myRange = range(cells(2,3),cells(10,15))
redeem myArray(1 to 20,1 to 20)
myArray=myRange
It's always much faster to work with variables and arrays than with cells values.

Using VBA to assign range of cell values to array of variables

I'm very new to VBA, to bear with me here.
I want to assign a set of variables the value of a set of ranges ie. run a brief code to simplify the following
Dim Sample 1 as string
Sample1 = activeworksheet.range("C17").value
Dim Sample 2 as string
Sample2 = activeworksheet.range("C18").value}
and so on
Following an excelfunctions.net tutorial, I know that I can shorten the declaration to
Dim Sample(1 to 20) as a string
But the tutorial drops it there(because it's a tutorial about names), suggesting I populate it as follows
sample(1)=activesheet.range("C7").value
sample(2)=activesheet.range("C7").value
and so on
I found the discussion below to be on the right track to answer my quest, but I am having trouble applying it to my situation. (Excel VBA Array Ranges for a loop)
As a follow up note, I am ultimately trying to assign values to these variables for use in the following procedures, rather than declaring and assigning them each time.
Thanks!
Try something like this:
Sub test()
Dim sampleArr(1 To 20) As String
Dim i As Integer
Dim rng As Range, cel As Range
i = 1
Set rng = Range("C1:C20")
For Each cel In rng
sampleArr(i) = cel.Value
i = i + 1
Next cel
For i = LBound(sampleArr) To UBound(sampleArr)
Debug.Print sampleArr(i)
Next i
Also, if you know the range you want to put into an array, you can simply set an array to that range:
Sub test()
Dim sampleArr() As Variant
Dim i As Integer
Dim rng As Range, cel As Range
i = 1
Set rng = Range("C1:C20") ' Note, this creates a 2 Dimensional array
sampleArr = rng ' Right here, this sets the values in the range to this array.
For i = LBound(sampleArr) To UBound(sampleArr)
Debug.Print sampleArr(i, 1) ' you need the ",1" since this is 2D.
Next i
End Sub
You should :
Define the range you want to retrieve data
For each cell of the range, retrieve your datas
dim tab() As string, cell as range, i as integer
i = 0
redim tab(0)
for each cell in ActiveWorksheet.Range("C1:C20")
tab(i) = cell
i = i + 1
redim preserve tab(i)
next
edit : I indent the code to display it correctly
Additional way to the above you can only use:
Arr = ActiveWorksheet.Range("C1:C20").Value
Then you can directly use:
Arr(i,1) where i is C1 to C20 range!

Creating and Transposing Array in VBA

I'm hoping to load values in a range to an array and transpose that array to another location (different workbook)
I am using the below forum post to get an idea of how to do it:
http://www.mrexcel.com/forum/excel-questions/629320-application-transpose-visual-basic-applications-array.html
Below is the code I am working with now, and I'm getting the 1004 object defined error. Can anyone spot what I am doing wrong?
I did find that the code works if I do not Set tRangeArray and instead do Sheets("sheet1").Range("C12:C19).Value = Application.Transpose(MyArray), but I'm not sure why that's different from my code.
Sub copy_data()
Dim cRange As Range, aRange As Range, tRange1 As Range, wbk1 As Workbook, wbk2 As
Workbook
Dim MyArray() As Variant, tRangeArray As Range
Set wbk1 = ThisWorkbook
MyArray = Range("E12:L12")
Set tRangeArray = wbk1.Sheets("sheet1").Range("C12:C19")
Sheets("sheet1").Range(tRangeArray).Value = Application.Transpose(MyArray)
As I mentioned in comments, just use:
tRangeArray.Value = Application.Transpose(MyArray)
Sheets("sheet1").Range(tRangeArray).Value not working, because Range accepts either single parameter - string with range address (not range itself): Range(addr), either two parameters - top left and bottom right cells: Range(cell_1,cell_2)
Similar, but using Resize and Ubound:
Dim myarray As Variant
myarray = Array(1, 2, 3, 4, 5)
Sheets("sheet1").Range("A1").Resize(UBound(myarray), 1).Value = Application.Transpose(myarray)

Resources