Printing array to variable cell range VBA - arrays

I have a variable array which I want to print into another workbook range. As I loop through separate arrays and secondary workbooks, the cell I want to print to will change.
Some sheets will need to array to be printed to A6:N, some will need the array printed to A300:N etc. I want to get to the bottom of the list of existing data and 'paste' the array below it.
Here is what I have so far:
bottomrow2 = Range("A9999").End(xlUp).Row
Set PasteCell = ClientBook.Sheets("PasteSheet").Range(Cells(bottomrow2 + 1,1),Cells(bottomrow2 + 1, 14))
Range(PasteCell & UBound(array)) = array
PasteCell is effectively trying to be my A1:N. It's worth noting that if I hard-code where to paste the array to, it works fine.
Range("A1:N" & UBound(array)) = array
'^This works fine.
Thanks for any help you can give.

You need to resize Paste Cell, so
Set PasteCell=PasteCell.Resize(ubound(array),14)
something like that. Or just use the anchor cell maybe, set paste cell to just be bottomrow2,1 ?

Related

Application of VBA control arrays in Access

I need to understand how I can relate the array of controls I dim in VBA code with the related form and the syntax etc. needed in the code to manipulate the array.
I'm wanting to populate and present an array of textboxes with strings that have been constructed as a result of processing data in a series of tables.
As an initial test, I've tried the following code. I've not yet thought of any way I might attempt to create an array on the surface of a form.
I have several text books on VBA but none of them seem to have anything to say on this.
Can anyone throw any light on this or recommend a more advanced text book?
Dim mytext(20) As TextBox
Dim x As Long
For x = 0 To 19
mytext(x).Value = str(x)
Next x
This results in an error at line 4:
Object variable or With block variable not set
How are you populating your array of Textboxes?
Since Textboxes are objects, you'll need to use Set, e.g.:
Dim mytext(20) As TextBox
Set mytext(0) = Text0
Set mytext(1) = Text2
Set mytext(2) = Text4
Set mytext(3) = Text6
...
Aside, Str is a built-in function in VBA, it should not be used as the name of a
variable.

Google Scripts: Populating Multi-Dimensional Array With Cell Values

I am learning the basics of Google Scripting after using VBA in Excel for many years, so please forgive the basic question.
I want to take a range of cells in my sheet using getValues() and then create a multi-element array.
Here is a simple example from my code:
var gameInfo = [];
gameInfo = gameMasterSheet.getRange(3, 3, 1, 9).getValues();
As you can see, the defined range is 9 cells in a single row.
My goal is to create an array from these 9 cells, and have each cell be accessible via a separate array element. However, it seems all 9 cell values are being inserted into gameInfo[0], and when I reference gameInfo[2] hoping to obtain the value of the third cell in the range, "undefined" is returned.
Is there a way to use getValues() to populate an array with separate elements? If so, how is this done? If not, what is a better alternative?
Thanks for any help you all can provide!
getValues returns a 2d array, even when it's a single row of cells. Think of it like this: gameInfo[row - 1][column - 1], so the top left is gameInfo[0][0].
All of your data is in gameInfo is in one row (gameInfo[0]), so the third element will be accessed as gameInfo[0][2] (row 1, column 3).
gameInfo[2] would be the third row, which is indeed outside of the range and undefined.
Also: to get just the values into an array from a 2d array, you could do this:
const values = [];
gameInfo.forEach(row => {
row.forEach(column => {
values.push(column);
})
})
See the script at the bottom of the Simple Mail Merge Tutorial for some useful code to retrieve data. Copy all the code from this comment down:
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects'
// tutorial.
//
/////////
/////////////////////////////////////////////////////////////////////////////////
// getRowsData iterates row by row in the input range and returns an array of objects.
Once you have, use getRowsData to retrieve the data as an array of objects. If I understand you correctly, this will get you want you are after.
Karl S

Excel refrencing array with adress and indirect from another work sheet

As part of a formula in Excel I am trying to create a reference to an array which is located on Another work sheet. In order to do so I am trying to combine INDIRECT and ADDRESS in this way:
INDIRECT(ADDRESS(MATCH('current sheet'!E87;'sheet 2'!$C$2:$C$47;0);1;1;1;"sheet 2"))&":"&INDIRECT(ADDRESS(47;1;1;1;"sheet 2"))
I receive the correct addresses when I paste the address formulas on their own, but once i combine them with indirect and try to put them in the formula down below it doesn't work. Does anyone understand what the problem might be?
=INDEX(INDIRECT(ADDRESS(MATCH('current sheet'!E87;'sheet 2'!$C$2:$C$47;0);1;1;1;"sheet 2"))&":"&INDIRECT(ADDRESS(47;1;1;1;"sheet 2"));MATCH('current sheet'!E87;'sheet 2'!$C$2:$C$47;0))
I think you don't need to append "sheet 2" on the second part of your array.
Just try this.
Go to your main sheet pick an epty cell, enter = then click on to your other sheet and draw the array you want to index.
It will look something like this: ='sheet 2'!E4:F12
At the moment your formula makes a reference that looks like this: 'sheet 21!E4:'sheet 2'!F12.
You don't need the second 'sheet 2'! in the array.
Hope this helps.

VBA - Get data of array inside another array

I have created an array that contains another array into it, but now I am struggling to get the data from it. Please see the image attached.
Imagine I would like to access the string in channelsData(1)(1,1)(0,0) so the first red crossed string. How can I call it in an If condition condition for example?
Thanks in advance.
To get the data is as #Rory suggested. channelsData(1,1)(0,0)
To get the dimension of the array at channelsData(1,1)(i) is UBound(channelsData(i, 1), 2)

How to copy cell to array in Matlab

Alright let me explain detailed my question
this below image is displaying my matrix where i want to copy my data
Alright now what i want to do that is as you can see 1x4 cell
i want to copy it as an array to another variable such as
input_values=ones(1,4);%init
input_values=input_matrix_training(1);
So at the above i am trying to copy the elements in that cell array which is row 1 to the input_values array. But if i do as i above i am getting this instead of the values that array contains. ty
instead of above it should be like
The other values are a cell, and are thus best referenced with {} instead of (). Also, sometimes they need to be wrapped into [], depending on the format. Plus the fact that you don't need to initialize input_values, and what you should do becomes this:
input_values=[input_matrix_training{1}];
Or you can just use cell2mat
input_values=cell2mat(input_values(1));

Resources