Arrays and data types

parry

MrExcel MVP
Joined
Aug 20, 2002
Messages
3,355
I have data in column A from row 1 downwards. All the data in Column A is a string being FileName1, FileName2 etc. However, even though the data in column A is a string when I try to put this directly into an array it comes up with a RunTime Error 13 - Type Mismatch.

If I change the array data type to a variant then it works. Why? I know you can have arrays of any type so why is this failing when the source data is of the same type as the array. Any clues would be appreciated.

This doesnt work
Code:
Sub Test1()
'THIS FAILS WITH RUN TIME ERROR 13: TYPE MISMATCH
Dim SourceArr() As String

'Place data source into an array
If IsEmpty([a65536]) Then
SourceArr = Range("a1:a" & Range("a65536").End(xlUp).Row)
Else
SourceArr = [a1:a65536]
End If

End Sub

This works
Code:
Sub Test2()
'THIS WORKS
Dim SourceArr() As Variant

'Place data source into an array
If IsEmpty([a65536]) Then
SourceArr = Range("a1:a" & Range("a65536").End(xlUp).Row)
Else
SourceArr = [a1:a65536]
End If

End Sub
 
Again, leverage the power of XL. Very few use more than a tiny fraction of XL's raw native capability. Sadly, too many resort to the all too familiar world of VBA too quickly.

Ignoring all the stuff about page numbers, suppose you have 65,000 entries in column A of Sheet1. Then, in Sheet2, select a range of 1,000 rows x 65 columns. Type the formula =INDIRECT("'Sheet1'!R"&((COLUMN()-1)*1000+MOD(ROW()-1,1000)+1)&"C1",FALSE) and complete data entry with CTRL+ENTER

Note that this is not an array formula, so do not use CTRL+SHIFT+ENTER.

On my computer this took the blink of an eye to complete.
parry said:
{snip}

On another forum an op asked to transfer data from one column into a matrix of 55 rows/9 columns per page for printing purposes. The eventual answer also included a page header as well. eg Page 1 of X in row 1 followed by 55 rows of data then Page 2 of X in row 57 etc. The matrix also has to read down 55 rows then onto the next column per page block.
{snip}
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Thanks Tushar, your quite right but I dont know a formula that could do it. Its not quite what the op wants though & because I dont understand your formula Im not sure how to amend it.

The op wants to get as many pages of 55 rows x 9 columns from source data in column A. To avoid errors of referencing cells off the sheet then lets say 7223 rows x 9 columns which is 65007 cells - close enough.

Also, your formula has the values listed all the way down the row until the bottom. He actually wants it to go down until row 55 then begin with the next value in the next column etc until at the bottom of column 9 it begins again.

Because of the constraints of the HTML maker I wont put all that but using an example of 5 rows x 3 columns then this is what it would look like. The names will not be filename1 etc.
Rugila.xls
ABCD
1FileName1FileName6FileName11
2FileName2FileName7FileName12
3FileName3FileName8FileName13
4FileName4FileName9FileName14
5FileName5FileName10FileName15
6FileName16FileName21FileName26
7FileName17FileName22FileName27
8FileName18FileName23FileName28
9FileName19FileName24FileName29
10FileName20FileName25FileName30
11FileName31FileName36FileName41
12FileName32FileName37FileName42
13FileName33FileName38FileName43
14FileName34FileName39FileName44
15FileName35FileName40FileName45
Sheet2
 
Upvote 0
Again, leverage the power of XL. Very few use more than a tiny fraction of XL's raw native capability. Sadly, too many resort to the all too familiar world of VBA too quickly.

Yes, of course You got a good point :biggrin:

The lack of knowledge of the built-in capacity is usually compensated with VBA.

However, from my point of view different problems need different solutions.

Setting up a table with 2000 VLOOKUP-formulas is not a good solution or using array-formulas in a 20x20 matris.

Many good and highly respected XL-users tend to use formulas/functions in an exaggerated way as well due to the lack of VBA-knowledge.

Four mainapproaches exist:
* Use the built-in tools like Pivottable etc.
* Use Formulas/Functions
* Use VBA
* Use other programming-languages to access XL, like VB/VB.Net /C# etc.

For professional solutions we usually use a mix of the above approaches.

Another point is that nowdays more and more users just want to have the solutions and then move on, ie quick & dirty solutions. No attention is paid about performance, efficiency or stability. Here we can strongly argue about the above. However, since the life-cycles of solutions tend to decrease the need to considerate these aspects are not worth the money, i e the payoff-time for an investment is too short.

Kind regards,
Dennis
 
Upvote 0
I don't know your math background, but the general rule for mapping a vector into a MxN matrix columnwise is that each matrix element (i,j) is given by the vector element (j-1)*M +((i-1) mod M+1), which can be simplified to (j-1)*M+i. :oops: That means that my original formula was overly complicated since it could be simplified to =INDIRECT("'Sheet1'!R"&((COLUMN()-1)*1000+ROW())&"C1",FALSE)

In the specific case of some number of MxN blocks, the rule becomes (j-1)*M+((i-1) mod M +1) + ((i-1) div M) * (M*N) Note that the first piece (j-1)*M+((i-1) mod M +1) is the same as that for creating a single block. So, select 7281 rows x 9 columns and enter the formula =INDIRECT("'Sheet1'!R"&((COLUMN()-1)*55+MOD((ROW()-1),55)+1+(INT((ROW()-1)/55)*(55*9)))&"C1",FALSE)
parry said:
Thanks Tushar, your quite right but I dont know a formula that could do it. Its not quite what the op wants though & because I dont understand your formula Im not sure how to amend it.

The op wants to get as many pages of 55 rows x 9 columns from source data in column A. To avoid errors of referencing cells off the sheet then lets say 7223 rows x 9 columns which is 65007 cells - close enough.

Also, your formula has the values listed all the way down the row until the bottom. He actually wants it to go down until row 55 then begin with the next value in the next column etc until at the bottom of column 9 it begins again.
{snip}
 
Upvote 0
Hi Dennis,
XL-Dennis said:
{snip}
Yes, of course You got a good point :biggrin:

The lack of knowledge of the built-in capacity is usually compensated with VBA.

However, from my point of view different problems need different solutions.

Setting up a table with 2000 VLOOKUP-formulas is not a good solution or using array-formulas in a 20x20 matris.

Many good and highly respected XL-users tend to use formulas/functions in an exaggerated way as well due to the lack of VBA-knowledge.
{snip}
Without a doubt. One needs a balance between the many approaches available. My bias has always been towards those that make the intent the most transparent, with machine efficiency becoming crucial only in limited cases. So, I do advocate the use of UDFs / macros when XL-native formulas become overly burdensome. IMO, many who hang out in the microsoft.public.excel.worksheet.functions group tend to overuse XL formulas, especially array formulas. Similarly, most who hang out here tend to overuse VBA.
XL-Dennis said:
{snip}
Another point is that nowdays more and more users just want to have the solutions and then move on, ie quick & dirty solutions. No attention is paid about performance, efficiency or stability. Here we can strongly argue about the above. However, since the life-cycles of solutions tend to decrease the need to considerate these aspects are not worth the money, i e the payoff-time for an investment is too short.
This is not a new phenomenon. Almost every manager I've dealt with in 20+ years knows that her/his review/bonus depends on *this* project *now*, not what happens to the project in the future. Consequently, no project is ever designed with maintenance in mind. Obviously, no one will say so, and the rationalization is that the project is 'short term.' Every one knows that that is not the case, but no better incentive system exists. Among the many projects I could discuss, here's one that comes to mind: In 1988 I was a consultant to this particular project team. I insisted that the project integrate Y2K considerations. The project manager swore on everything holy that that was a waste of resources because the project was designed to last no more than 5 years. I bailed out of the project and, subsequently, moved on to other endeavors. Talking with a friend a couple of years ago, I learnt that in 1999 the company made a multi-million acquisition of a new system that duplicated the functionality of the '5 year' project but was Y2K compatible!
 
Upvote 0
Hi Tusharm,

I believe that the local terms of management around the world have becomed more similar nowdays then in the past and therefore there exist a "timelag" between our experiences :biggrin:

I´m not total convinced that the US-managementsystem fits in everywhere but it something we all have to accept and live with.

Kind regards,
Dennis
 
Upvote 0
tusharm said:
I don't know your math background, but the general rule for mapping a vector into a MxN matrix columnwise is that each matrix element (i,j) is given by the vector element (j-1)*M +((i-1) mod M+1), which can be simplified to (j-1)*M+i. :oops: That means that my original formula was overly complicated since it could be simplified to =INDIRECT("'Sheet1'!R"&((COLUMN()-1)*1000+ROW())&"C1",FALSE)

In the specific case of some number of MxN blocks, the rule becomes (j-1)*M+((i-1) mod M +1) + ((i-1) div M) * (M*N) Note that the first piece (j-1)*M+((i-1) mod M +1) is the same as that for creating a single block. So, select 7281 rows x 9 columns and enter the formula =INDIRECT("'Sheet1'!R"&((COLUMN()-1)*55+MOD((ROW()-1),55)+1+(INT((ROW()-1)/55)*(55*9)))&"C1",FALSE)

Thanks Tushar, at first I was confused by the Sheet1!R piece which appears to be looking in Column R but on reading help on the Indirect function I see you are using an R1C1 reference.

Given your comments then could this formula not be shortened from...
(j-1)*M+((i-1) mod M +1) + ((i-1) div M) * (M*N)
to
(j-1)*M+i + ((i-1) div M) * (M*N)

It appears not because when I tried the shortened version I got an error on the 'False' argument...
=INDIRECT("'Sheet1'!R"&((COLUMN()-1)*55+ROW())+(INT((ROW()-1)/55)*(55*9)))&"C1",FALSE)
 
Upvote 0
No, the formula can be shortened only in the instance when there is a single block of M rows.

Edit: And, that is because for a single block the largest value of i -- which is the same as ROW() -- will be M, so (i-1) mod M will always equal i-1. Hence, (i-1) mod M + 1 will always equal (i-1)+1 or i.

That is not true when i can be > M.

parry said:
{snip}

Thanks Tushar, at first I was confused by the Sheet1!R piece which appears to be looking in Column R but on reading help on the Indirect function I see you are using an R1C1 reference.

Given your comments then could this formula not be shortened from...
(j-1)*M+((i-1) mod M +1) + ((i-1) div M) * (M*N)
to
(j-1)*M+i + ((i-1) div M) * (M*N)

It appears not because when I tried the shortened version I got an error on the 'False' argument...
=INDIRECT("'Sheet1'!R"&((COLUMN()-1)*55+ROW())+(INT((ROW()-1)/55)*(55*9)))&"C1",FALSE)
 
Upvote 0
Thanks again Tushar, bear with me once more please just so I am crystal clear on this.

Single MxN Matrix
(j-1)*M +((i-1) mod M+1) OR (j-1)*M+i

Repeated MxN Matrix
(j-1)*M+((i-1) mod M +1) + ((i-1) div M) * (M*N)

Where…
i = Source Row Number - i.e. Row()
j = Source Column Number - i.e. Column()
M = Number of rows in Matrix ( y axis for the Matrix )
N = Number of columns in the Matrix (x axis for the Matrix)

To convert this into a formula expression so its easier to understand...
Formula Expression of Single Matrix
INDIRECT(“R” & ((Column()-1)*M+Row())&”C”&Y,False)

Formula Expression of Repeated Matrix
=INDIRECT("R"&((COLUMN()-1)*M+MOD((ROW()-1),M)+1+(INT((ROW()-1)/M)*(M*N)))&"C”&Y,FALSE)

Where…
M = Number of rows in Matrix
Y = Number of columns in the source.
N = Number of columns in the Matrix

So I only need to change these arguments – correct?

cheers (y)
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,679
Members
449,463
Latest member
Jojomen56

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top